我有一个文本文件,其中包含一个巨大的行号列表,我必须从另一个主文件中删除它。这是我的数据的样子
行.txt
1
2
4
5
22
36
400
...
and documents.txt
string1
string2
string3
...
如果我有一个简短的行号列表,我可以轻松使用
sed -i '1d,4d,5d' documents.txt
.
但有很多行号我必须删除。另外,我可以使用 bash/perl 脚本将行号存储在数组中,并回显不在数组中的行。但我想知道是否有一个内置命令可以做到这一点。
任何帮助将不胜感激。
awk oneliner 应该适合你,请参阅下面的测试:
kent$ head lines.txt doc.txt
==> lines.txt <==
1
3
5
7
==> doc.txt <==
a
b
c
d
e
f
g
h
kent$ awk 'NR==FNR{l[$0];next;} !(FNR in l)' lines.txt doc.txt
b
d
f
h
正如莱文建议的那样,我添加一些解释:
awk # the awk command
'NR==FNR{l[$0];next;} # process the first file(lines.txt),save each line(the line# you want to delete) into an array "l"
!(FNR in l)' #now come to the 2nd file(doc.txt), if line number not in "l",print the line out
lines.txt # 1st argument, file:lines.txt
docs.txt # 2nd argument, file:doc.txt
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)