使用perl

时间:2016-03-25 20:50:30

标签: perl

我正在尝试从一个文件中打印几行,其中行在不同的行中。我不认为以下perl是最好的方式,但希望它是一个开始

perl -ne 'if ($. == 5) ($. == 6) ($. == 7) ($. == 10) {print;exit}' file.txt

文件

#Sample = xxxxx
#Sample Type = xxxxxx
#Build = xxxxxxx
#Platform = xxxxxxx
#Display Name= XXXXX  (keep this field without the #)
#identifier = xxxxxx  (keep this field without the #)
#Gender = xxxxx       (keep this field without the #)
#Control Gender = xxxxx
#Control Sample = xxxxx
#Quality = X.XXXXXX   (keep this field without the # and X.XXX)

所需的输出

Display Name= XXXXX  (keep this field without the #)
identifier = xxxxxx  (keep this field without the #)
Gender = xxxxx       (keep this field without the #)
Quality = X.XXXXXX   (keep this field without the # and X.XXX)

3 个答案:

答案 0 :(得分:1)

您可以使用'smartmatch'操作符执行此任务:

perl -ne 's/^#//; @n = (5, 6, 7, 10); print if $. ~~ @n'  file.txt

答案 1 :(得分:1)

不是指定要保留的行数,而是通过它们包含的文本来识别它们更清楚

喜欢这个

perl -ne 'print $1 if /^#((Display Name|identifier|Gender|Quality).*)/is' myfile

输出

Display Name= XXXXX
identifier = xxxxxx
Gender = xxxxx
Quality = X.XXXXXX

我不明白你对最后一行数据的评论是什么意思"保留这个字段没有#和X.XXX" 。你是说"没有# X.XXX"?如果你只想在这里Quality =

那就很奇怪

答案 2 :(得分:0)

perl -ne 'print if $.==5 || $.==6 || $.==7 || $.==10' file.txt