使用UNIX删除文本文件中的某些行 [英] Delete certain rows in text files using UNIX
本文介绍了使用UNIX删除文本文件中的某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆需要清理的文本文件.我正在使用UNIX bash,因此AWK或grep很好.
I have a bunch of text files that needs cleaning. Im using UNIX bash, so AWK or grep is good.
文本文件如下所示:
1766 1789
1764 1790
1762 1849
0
1357 1817
1366 1857
0
360 42
352 95
0
293 142
302 181
delete-this
0
302 181
0
我要删除的所有行都带有"0",删除此",只有两行或三行两列.
What I want is to delete all rows with "0", "delete-this", only one row with two columns or three rows with two columns.
结果应如下所示:
1766 1789
1762 1849
1357 1817
1366 1857
360 42
352 95
293 142
302 181
非常感谢!
更多信息:第1行第2列和第2行第2列之和应大于1,否则,必须删除第2行.
More info: The sum of row 1 column 2 and row 2 column 2 should be >1, if not, row 2 must be deleted.
推荐答案
这很难说,或者很难理解,但是在这里我们再次尝试:
This was a hard nut, or difficult to understand, but here we go again:
awk '/[0-9]+ [0-9]+/ {a[++t]=$0;b[t]=$2;next} {if (t>=2) for (i=1;i<=t;i++) {if (b[i]-c!=1) print a[i];c=b[i]};t=0}'
1766 1789
1762 1849
1357 1817
1366 1857
360 42
352 95
293 142
302 181
它是如何工作的:
How does it work:
awk '
/[0-9]+ [0-9]+/ { # if line does have 2 column of number, then
a[++t]=$0 # add line to array "a" and increment variable "t"
b[t]=$2 # add column 2 to array "b"
next # go to next line
}
{
if (t>=2) # is there more two or more lines with numbers connrected, then
for (i=1;i<=t;i++) { # loop trough array "a" with all numbers
if (b[i]-c!=1) # test if the difference between this number in column 2 is more than 1 compare to previous line
print a[i] # then print array "a"
c=b[i] # store array "b" information in variable "b"
}
;t=0 # clear counter "t"
}' file
这篇关于使用UNIX删除文本文件中的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文