我有一个非常大的文件(下面的片段)。我需要删除第一列中的数字不从上面的行连续增加的任何行。
例如,我想保留代码片段中的第一行,其中第一列中的标识符是“ 40812
”。然后我想保留40813
第一列中“ ”所在的行(在我的示例中为第 3 行),然后保留以“ ,”开头的行,40814
依此类推。我想删除任何违反此顺序的行,例如第二行。
我在这里查看了之前的问题/答案以寻找可能的解决方案,但到目前为止还没有成功。几个问题中都出现过的一个解决方案是:
awk -F',' ' '!seen[$1]++ myFile
我采用了我看到的另一个解决方案:
sort -t':' -k 1,1 -u myFile
如果有人能告诉我哪里出错了,我将不胜感激。我对文件操作不太有经验。
40812 20406.000000 0.843859468 1083.209050130 -994.562279080 -993.349611938 22.120868921
40829 20414.500000 0.891283743 1144.084593627 -994.539001565 -993.349739827 21.177788019
40813 20406.500000 0.829362077 1064.599666089 -994.546948121 -993.348764740 22.087239027
40830 20415.000000 0.889606427 1141.931529727 -994.537943593 -993.350242614 21.282490969
40814 20407.000000 0.822524589 1055.822814442 -994.540118434 -993.348757318 22.083606005
40831 20415.500000 0.875230513 1123.478077086 -994.523844766 -993.350421831 20.606467962
40815 20407.500000 0.823511602 1057.089780943 -994.541681744 -993.349315083 22.432111979
40832 20416.000000 0.846150258 1086.149592126 -994.494220141 -993.349798791 22.309054136
40816 20408.000000 0.824550451 1058.423286012 -994.543159511 -993.349731194 22.481428146
40833 20416.500000 0.811604775 1041.805740021 -994.458563132 -993.348626225 21.118428946
40834 20417.000000 0.787796672 1011.244783236 -994.434062658 -993.347887110 20.963790894
40817 20408.500000 0.819160081 1051.504008955 -994.537767061 -993.349702160 22.268819809
40835 20417.500000 0.784857495 1007.471947645 -994.431441227 -993.348167742 20.731789112
40818 20409.000000 0.807571275 1036.628191427 -994.525675417 -993.349169067 22.332761049
40836 20418.000000 0.799208319 1025.893192994 -994.446595759 -993.348938468 21.268665075
40819 20409.500000 0.797104599 1023.192780242 -994.514563564 -993.348491176 22.622548103
40837 20418.500000 0.819797939 1052.322786256 -994.467698852 -993.349417295 21.013041973
40820 20410.000000 0.796605925 1022.552664951 -994.513928312 -993.348319789 22.193170071
答案1
这正是我们所擅长的事情awk
:
$ awk '{ if(NR==1 || $1 == last+1){print; last=$1}}' file
40812 20406.000000 0.843859468 1083.209050130 -994.562279080 -993.349611938 22.120868921
40813 20406.500000 0.829362077 1064.599666089 -994.546948121 -993.348764740 22.087239027
40814 20407.000000 0.822524589 1055.822814442 -994.540118434 -993.348757318 22.083606005
40815 20407.500000 0.823511602 1057.089780943 -994.541681744 -993.349315083 22.432111979
40816 20408.000000 0.824550451 1058.423286012 -994.543159511 -993.349731194 22.481428146
40817 20408.500000 0.819160081 1051.504008955 -994.537767061 -993.349702160 22.268819809
40818 20409.000000 0.807571275 1036.628191427 -994.525675417 -993.349169067 22.332761049
40819 20409.500000 0.797104599 1023.192780242 -994.514563564 -993.348491176 22.622548103
40820 20410.000000 0.796605925 1022.552664951 -994.513928312 -993.348319789 22.193170071
或者,打一点高尔夫球:
$ awk '(NR==1 || $1 == last+1) && last=$1' file
40812 20406.000000 0.843859468 1083.209050130 -994.562279080 -993.349611938 22.120868921
40813 20406.500000 0.829362077 1064.599666089 -994.546948121 -993.348764740 22.087239027
40814 20407.000000 0.822524589 1055.822814442 -994.540118434 -993.348757318 22.083606005
40815 20407.500000 0.823511602 1057.089780943 -994.541681744 -993.349315083 22.432111979
40816 20408.000000 0.824550451 1058.423286012 -994.543159511 -993.349731194 22.481428146
40817 20408.500000 0.819160081 1051.504008955 -994.537767061 -993.349702160 22.268819809
40818 20409.000000 0.807571275 1036.628191427 -994.525675417 -993.349169067 22.332761049
40819 20409.500000 0.797104599 1023.192780242 -994.514563564 -993.348491176 22.622548103
40820 20410.000000 0.796605925 1022.552664951 -994.513928312 -993.348319789 22.193170071
解释
if(NR==1 || $1 == last+1)
:NR
是当前行号。因此NR == 1
仅在读取文件的第一行时才成立。我们需要这个,所以我们总是打印第一行。然后,$1 == last +1
如果该行 ($1
) 的第一个字段等于存储在变量中的值last
加 1,则为 true。总而言之,这意味着“如果这是最后一行或者如果第一个字段等于 last + 1”,其中定义您的目标线。print; last=$1
:如果上述两个条件之一为真,则打印该行并将 的值设置last
为第一个字段这行,以便我们可以处理下一个。