删除与行号从文本文件行从另一个文件 [英] Delete line from text file with line numbers from another file

查看:204
本文介绍了删除与行号从文本文件行从另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我必须从另外一个主文件中删除行号的巨型列表的文本文件。下面是我的数据看起来像

I have a text file containing a giant list of line numbers which I have to remove from another main file. Here's what my data looks like

lines.txt

lines.txt

1
2
4
5
22
36
400
...

documents.txt

string1
string2
string3
...

如果我有行号的短名单,我可能已经很容易地使用

If I had a short list of line numbers I could've easily used

SED -i1D,4D,5D'documents.txt

但也有很多很多的行号,我要删除。另外,我可以使用bash / perl脚本的行号存储在一个阵列和回声这是不数组中的线条。但我在想,如果有一个内置的命令来做到这一点。

But there are lots of lots of line number that I have to delete. Also, I could use bash/perl script to store the line numbers in an array and echo the lines which are not in the array. But I was wondering if there is a built in command to do just that.

任何帮助将是非常美联社preciated。

Any help would be highly appreciated.

推荐答案

AWK oneliner应该为你工作,请参见下面的测试:

awk oneliner should work for you, see test below:

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

作为列翁的建议,我添加一些说明:

as Levon suggested, I add some explanation:

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

这篇关于删除与行号从文本文件行从另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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