模式解码 [英] Pattern decoding

查看:132
本文介绍了模式解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在以下一点帮助。我有这样的数据文件:

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 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

0 0    # <--- Group 6

有一些情况下,这必须回答:

There are some cases, which have to be answered:

有在示例团;存在的基团,如果它是通过从其他新行分隔,所以在这种情况下,我们有6组。我们要确定以下

There are groups in the example; there exists a group, if it is separated by new lines from the other, so in this case we have 6 groups. We have to determine the followings

获取该集团的实际数目(序号)(计数器开始如1)

Get the actual number (ordinal number) of the group (the counter starts for example from 1)


  • 如果第1列= 0和第2列= 0和下一行是空的
    所以根据上面的例子中所需的输出将是

  • if the 1st column = 0 and the 2nd column = 0 and the next line is empty So the desired output according to the above example would be

1 结果
5 结果
6

如果第一列= 0和第2列可以变化,下一行是空的
所以根据上面的例子中所需的输出将是

if the first column = 0 and the 2nd column can vary and the next line is empty So the desired output according to the above example would be

3

...等,这怎么能在我们可以设置在这种情况下,我们会希望得到之初的方式来推广?
有可能是根据一组中的列的值许多情况。

... etc. How can this be generalized in a way that we can set at the beginning which case would we like to get ? There might be many cases according to the values of the columns in a group.

如果我们想到这样的事情,我们可以想像这样的:第一列是指在街道房屋的数量,第二列是指房间的门牌号码。现在我想找到一个城市的所有可能样的街道,这意味着例如:

We can imagine this if we think about something like this: the first column means the number of houses in a street, and the second column means the number of rooms in a house. Now I would like to find all possible kind of streets in a city, which means for example

让我们拿起这些街道,其中有两院不同的房间数,在第一间房子有3个房间,并在第二个房子有2个房间。因此,我们必须在获取输出的 2 ,becasue这个要求满足该组的文件中的

let us pick up those streets, in which there are two houses with different number of rooms, in the first house there are 3 rooms, and in the second house there are 2 rooms. So we have the get output 2, becasue this requirement fulfills this group in the file

0 0
0 1
0 2
1 0
1 1

重要: 0 表示有一个房子,有一个房间

Important: 0 0 means there is one house with one room

校正:如果有一个房子而已,那么它只有一个房间里所有的时间!像在例1组,第5组,和6组记住,第二列是房间的数目,而0意味着1室,1表示2房间,...等。这仅仅是从0开始,而不是1,对不起,如果这是混淆了一点点反...

Correction: if there is one house only, then it has just one room all the time! Like in the cases Group 1, Group 5, and Group 6. Remember, that the second column is the number of room, and 0 mean "1 room", 1 means "2 rooms", ...etc. This is just a counter which starts from 0, instead of 1, sorry if it is confusing a little bit...

推荐答案

我不知道什么是你期望的输出,但是我已经转换/解codeD的号码模式,以一种有意义的组/家/间格式。任何进一步的查询可以在这个内容来完成。

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.

见下图:

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

我们得到的输出:

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"

更新

OP的评论:

我需要知道,其中有/具有例如组(多个)的数量
  1房子,有一个房间。输出将是在上述情况下:1,5,6

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

正如我所说,根据您的查询条件,我们可以调整下一步awk的输出。现在我改变AWK abovet为:

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

这将输出:

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

格式为g roupIdx houseIdx numberOfRooms 并有组之间的空行。我们上面保存到指定文件中的文本德coded.txt

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

上述装置的最后一个AWK线,打印组号,如果房间号($ 3)= 1,并且只有一个在该组块行。

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.

这篇关于模式解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆