【发布时间】:2016-12-23 13:01:53
【问题描述】:
我是 perl 脚本的新手,需要有关给定问题的帮助。 我有许多包含人员详细信息的文件。 我想在特定行之后和特定行之前打印每个文件的内容。 示例:(其中一个文件包含以下详细信息:)
My name is XYZ.
Address: ***
ID:12414
Country:USA
End XYZ
另一个文件包含以下详细信息:
My name is ABC.
Address: ###
ID:124344
Country:Singapore
End ABC
我想将My name is XYZ 之后和End XYZ 之前的第一个文件中的行打印到我的新文件中。同样,我想将My name is ABC 之后和End ABC 之前的第二个文件中的内容打印到我的新文件中。
我编写了如下逻辑,但我不确定在特定行之后和下方打印的 perl 语法。
while(<file1>)
{
if () # if we read the phrase "My name" in file1 start printing after this +line
{
print #print the contents into file3(output file)
if() # if we read the phrase "End" in file1 stop printing the content into +file3
}
}
我希望我的问题很清楚。任何帮助表示赞赏。
【问题讨论】:
-
(/^My name is/../^End/) !~ /^1?$|E/ and print while <file1> -
@ysth 在您的模式中
1表示行号,那么您为什么使用^和$? -
@mkHun 不,它与
..的返回匹配;见perldoc.perl.org/perlop.html#Range-Operators和perlmonks.org/?node_id=525406 -
有谁知道为什么 所有 答案——即使是被接受的答案——都被否决了?他们都在 cmets 中得到了积极的反馈。我知道每个人都可以根据自己的喜好投票,但这似乎不合理。