1

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

I tried this:

> perl -ne "print if $. < 10" in.csv >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


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:

>

I have to stop the program using Ctrl+c.

Using the simple csv-File like ikegami (just some rows) the onliner works. I assume there is something within the csv-file.

Update 2:
The original onliner:

> perl -ne "print if $. <= 10" in.csv >out.txt

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

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.

giordano
  • 2,954
  • 7
  • 35
  • 57

1 Answers1

2

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
ikegami
  • 367,544
  • 15
  • 269
  • 518