Pattern decoding

后端 未结 3 1859
情话喂你
情话喂你 2021-01-16 03:02

I need a little help in the following. I have this kind of datafile:

0 0    # <--- Group 1 -- 1 house (0) and 1 room (0)

0 0    # <--- Group 2 -- 2 ho         


        
3条回答
  •  不要未来只要你来
    2021-01-16 03:11

    I don't know what would be your expected output, however I have converted/decoded your number pattern to a meaningful group/house/rooms format. any further "query" could be done on this content.

    see below:

    kent$  cat file
    0 0
    
    0 0
    0 1
    0 2
    1 0
    1 1
    
    0 0
    0 1
    0 2
    
    0 0
    1 0
    2 0
    3 0
    
    0 0
    
    0 0
    

    awk:

    kent$  awk 'BEGIN{RS=""} 
            { print "\ngroup "++g; 
            delete a;
            for(i=1;i<=NF;i++) if(i%2) a[$i]++;
            for(x in a) printf "House#: %s , Room(s): %s \n", x, a[x]; }' file
    

    we get output:

    group 1
    House#: 0 , Room(s): 1 
    
    group 2
    House#: 0 , Room(s): 3 
    House#: 1 , Room(s): 2 
    
    group 3
    House#: 0 , Room(s): 3 
    
    group 4
    House#: 0 , Room(s): 1 
    House#: 1 , Room(s): 1 
    House#: 2 , Room(s): 1 
    House#: 3 , Room(s): 1 
    
    group 5
    House#: 0 , Room(s): 1 
    
    group 6
    House#: 0 , Room(s): 1 
    

    note that the generated format could be changed to fit your "filter" or "query"

    UPDATE

    OP's comment:

    I need to know, the number of the group(s) which have/has for example 1 house with one room. The output would be in the above case: 1, 5 ,6

    as I said, based on your query criteria, we could adjust the awk output for next step. now I change the awk abovet to:

    awk 'BEGIN{RS=""} 
            {print "";  gid=++g; 
            delete a;
            for(i=1;i<=NF;i++) if(i%2) a[$i]++;
            for(x in a) printf "%s %s %s\n", gid,x, a[x]; }' file
    

    this will output:

    1 0 1
    
    2 0 3
    2 1 2
    
    3 0 3
    
    4 0 1
    4 1 1
    4 2 1
    4 3 1
    
    5 0 1
    
    6 0 1
    

    the format is groupIdx houseIdx numberOfRooms and there is a blank line between groups. we save the text above to a file named decoded.txt

    so your query could be done on this text:

    kent$  awk 'BEGIN{RS="\n\n"}{if (NF==3 && $3==1)print $1}' decoded.txt
    1
    5
    6
    

    the last awk line above means, print the group number, if room number ($3) = 1 and there is only one line in the group block.

提交回复
热议问题