Perl一行最后添加文本但一行大文件
我是 Perl 的新手。请帮助我使用一个班轮或 Perl proc 或 Perl 程序进行编程。假设我的输入文件是 input.txt,其内容如下:
This is an example
This file has three lines
Oh you are mistaken. It has many lines
I want my text here
Thanks for making it to the last line of input.txt.
下面是我要生成的输出文件:
This is an example
This file has three lines
Oh you are mistaken. It has many lines
I want my text here
This line has special characters like $
I love this community
Thanks for making it to the last line of input.txt
我在 tcsh 上运行这个。我使用了下面的单线:
Perl -p -e 'print "This line has special characters like $ nI love this community"' if $. == 9' input.txt > output.txt
问题是,在上面的例子中,我知道最后一行的编号。但是在我的代码中,input.txt 的长度一直在变化。我应该对单行进行哪些更改,以便即使我不提供最后一行编号也能正常工作。
注意:请不要建议使用 sed。我尝试使用 sed 并且我成功地执行了所需的任务。但是,我的输入文件大约为 325MB,而 sed 需要 25 分钟才能完成此任务。我希望它在不到 5 分钟的时间内完成。
正在使用的 Perl 版本:v5.10.1
回答
而不是固定行号,检查它是否是输入文件的结尾 eof
perl -pe 'print "This line has special characters like $ nI love this communityn" if eof' input.txt > output.txt