Pattern decoding II [duplicate]

2019-09-03 03:37发布

Possible Duplicate:
Pattern decoding

I have some new question concerning to the previous post about pattern decoding:

I have almost the same data file, BUT there are double empty (blank) lines, which have to be taken into account in the decoding. So, the double empty lines mean that there was a street/grout (for definitions see the previous post: Pattern decoding) in which there was zero (0) house, but we have to count these kind of patterns too. (Yes, you may think, that this is absolutely wrong statement, because there is no street without at least one house, but this is just an analogy, so please, just accept it as it is.)

Here is the new data file, with the double lines:

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

0 0    # <--- Group 2 -- 2 houses (0;1) and 3,2 rooms (0,1,2;0,1)
0 1
0 2    
1 0    # <--- house 2 in Group 2, with the first room (0)
1 1    # <--- house 2 in Group 2, with the second room (1)

0 0    # <--- Group 3
0 1    # <--- house 1 in Group 3, with the second room (1)
0 2

0 0    # <--- Group 4
1 0    # <--- house 2 in Group 4, with one room only (0)
2 0
3 0    # <--- house 4 in Group 4, with one room only (0)

0 0    # <--- Group 5
       # <--- Group 6 << ---- THIS IS THE NEW GROUP

0 0    # <--- Group 7
       # <--- Group 8 << ---- THIS IS THE NEW GROUP

0 0    # <--- Group 9

0 0    # <--- Group 10 

I need to convert this into an elegant way as it has been done before, but in this case we have to take into account these new groups too, and indicate them in this way, following Kent for example: roupIdx houseIdx numberOfRooms, where the houseIdx let equal to zero houseIdx = 0 and the numberOfRooms let equal to zero too numberOfRooms = 0. So, I need to get this kind of output for example:

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 0

7 0 1

8 0 0

9 0 1

10 0 1

Can we tune the previous code in this way?

UPDATE: the new second empty line indicates a new group. If there was an additional empty new line after the empty line, as in this case

0 0    # <--- Group 5
       # <--- Group 6 << ---- THIS IS THE NEW GROUP

0 0    # <--- Group 7
       # <--- Group 8 << ---- THIS IS THE NEW GROUP

we just treat the new empty line (the second one in the 2 blank lines) as a new group, and indicate them as group_index 0 0. See the desired output above!

1条回答
Viruses.
2楼-- · 2019-09-03 04:19

Try:

$ cat houses.awk
BEGIN{max=1;group=1}
NF==0{
   empty++
   if (empty==1) group++
   next
}
{ max = ($1 > max) ? $1 : max
   if (empty<=1){
        a[group,$1]++ 
   } else {
        a[group,$1]=-1
   }
  empty=0
}
END{for (i=1;i<=group;i++){
        for (j=0;j<=max;j++){
            if (a[i,j]>=1)
                print i , j , a[i,j]
            if (a[i,j]==-1)
                print i, j, 0
        }
        printf "\n"
    }
}

Command:

awk -f houses.awk houses

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 0

7 0 0

8 0 1
查看更多
登录 后发表回答