Perl 一个衬里最后添加文本但是一个大文件的一行
Perl one liner to add text at last but one line of a large file
我是 Perl 新手。请帮助我使用一个 liner 或 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 的长度不断变化。我应该对 one-liner 进行哪些更改,以便即使我不提供最后一行编号也能正常工作。
注意:请不要建议使用sed。我尝试使用 sed 并成功执行了所需的任务。但是,我的输入文件大约有 325MB,而 sed 需要 25 分钟才能完成这项任务。我希望它能在 5 分钟内完成。
正在使用的 Perl 版本:v5.10.1
而不是固定的行号,用eof
检查它是否是输入文件的结尾
perl -pe 'print "This line has special characters like $ \nI love this community\n" if eof' input.txt > output.txt
使用 GNU sed
在输入的最后一行之前插入文本:
sed '$i This line has special characters like $\nI love this community' input.txt > output.txt
我是 Perl 新手。请帮助我使用一个 liner 或 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 的长度不断变化。我应该对 one-liner 进行哪些更改,以便即使我不提供最后一行编号也能正常工作。
注意:请不要建议使用sed。我尝试使用 sed 并成功执行了所需的任务。但是,我的输入文件大约有 325MB,而 sed 需要 25 分钟才能完成这项任务。我希望它能在 5 分钟内完成。
正在使用的 Perl 版本:v5.10.1
而不是固定的行号,用eof
perl -pe 'print "This line has special characters like $ \nI love this community\n" if eof' input.txt > output.txt
使用 GNU sed
在输入的最后一行之前插入文本:
sed '$i This line has special characters like $\nI love this community' input.txt > output.txt