Perl oneliner:将第一行打印到文件中 [英] Perl oneliner: print first lines into file
问题描述
我想将一个大文件的前10行打印到一个新文件中.有了这个oneliner,我可以在监视器上打印行:
I want to print the first 10 lines of a huge file into a new file. With this oneliner I can print the lines on the monitor:
> perl -ne "print if $. < 10" in.csv
我尝试过:
> perl -ne "print if $. < 10" in.csv >out.txt
但这只会创建文件out.txt而不会在其中写入第一行.此代码有什么问题?
感谢您的帮助
But this does only create the file out.txt without writing the first lines into it. What is wrong with this code?
Thanks for help
Windows 7/Strawberry Perl
Windows 7 / Strawberry Perl
更新1:
如果我使用以下方法在监视器上发送打印结果:
Update1:
If I send the print result on monitor using:
> perl -ne "print if $. <= 10" in.csv
程序不会停止,也就是说,前十行是在监视器上输出的,但不会以以下结尾:
the program does not stop, that is, the first ten lines are output on the monitor but it does not end with:
>
我必须使用Ctrl + c停止该程序.
I have to stop the program using Ctrl+c.
使用像ikegami这样的简单csv文件(仅几行),onliner即可工作.我认为csv文件中有东西.
Using the simple csv-File like ikegami (just some rows) the onliner works. I assume there is something within the csv-file.
更新2:
原始在线用户:
Update 2:
The original onliner:
> perl -ne "print if $. <= 10" in.csv >out.txt
有效.我要等几秒钟.csv文件的大小为2 GB.联机人员:
works. I have to wait some seconds. The csv-file is 2 GB large. The onliner:
> perl -pe "exit if $. > 10" in.csv >out.txt
立即给出结果.结论:第一个在线用户浏览所有行,第二个在线用户退出10行.
gives the result immediately. Conclusion: the first onliner goes through all rows, the second exit after 10 rows.
对不起,我对此问题感到困扰.我吸取了教训:使用适当的在线服务或等待几秒钟.
Sorry that I bother you with this problem. I learnt my lesson: use an appropriate onliner or wait some more seconds.
推荐答案
哼?正确(除了< 10
应该是< = 10
):
hum? That's correct (except < 10
should be <= 10
):
>type in.csv
a
b
c
d
e
f
g
h
i
j
k
l
m
n
...
z
>perl -ne "print if $. <= 10" in.csv >out.txt
>type out.txt
a
b
c
d
e
f
g
h
i
j
>
当不再需要打印而不是打印整个文件时,将退出更快的解决方案.
A faster solution would exit when it has no more to print rather than printing the entire file.
perl -pe"last if $. > 10" in.csv >out.txt
这篇关于Perl oneliner:将第一行打印到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!