[英]Remove lines from file
I am doing some text processing on a unix system. 我正在unix系统上进行一些文本处理。 I have access to the command line on this machine and it has Python, Perl and the default text processing progams installed, awk etc. 我可以访问这台机器上的命令行,并且它具有Python,Perl和默认的文本处理程序,awk等。
I have a text file that looks like below: 我有一个文本文件,如下所示:
2029754527851451717
2029754527851451717
2029754527851451717
2029754527851451717
2029754527851451717
2029754527851451717 1232453488239 Tue Mar 3 10:47:44 2009
2029754527851451717 1232453488302 Tue Mar 3 10:47:44 2009
2029754527851451717 1232453488365 Tue Mar 3 10:47:44 2009
2895635937120524206
2895635937120524206
2895635937120524206
2895635937120524206
2895635937120524206
2895635937120524206
5622983575622325494 1232453323986 Thu Feb 12 15:57:49 2009
It is basically 3 rows: ID ID Date 它基本上是3行:ID ID Date
I am looking to remove all the lines that do not have 2 ID's and a Date. 我想删除所有没有2个ID和一个Date的行。 So the finising results will be like this: 因此,优化结果将如下所示:
2029754527851451717 1232453488239 Tue Mar 3 10:47:44 2009
2029754527851451717 1232453488302 Tue Mar 3 10:47:44 2009
2029754527851451717 1232453488365 Tue Mar 3 10:47:44 2009
5622983575622325494 1232453323986 Thu Feb 12 15:57:49 2009
How would you guys suggest doing this? 你们会建议这样做吗? In total the text file is around 30,000 lines long. 文本文件总共约30,000行。
Cheers 干杯
Eef ef
用awk
:
awk 'NF > 2' input_file > output_file
grep ':' filename
with open(source_filename) as src:
with open(dest_filename, 'w') as dst:
for line in src:
if len(line.split()) > 1:
dst.write(line)
使用Perl:
perl -ne 'print if /^([0-9]+\s+){2}.+$/' $filename
只是在这里工作我的perl,但这可能也会有所帮助:
perl -lane 'if (scalar(@F) == 3) { print @F;}' file >> file.out
With Python: 使用Python:
file = open(filename, 'r')
lines = file.readlines()
file.close()
p = re.compile('^\d*$')
for line in lines:
if not p.search(line): print line,
perl -i -lane 'print if($F[1])' file
awk "NF>1" < filename
sed '/^[0-9]$/d' filename
(might have to modify the pattern if the bad lines have trailing spaces). (如果坏行末尾有空格,则可能必须修改模式)。 You can also use grep -v, which will omit the matched pattern. 您也可以使用grep -v,它将省略匹配的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.