A little perl
I haven't posted any perl in a while(I haven't posted anything in a while).
I have an occasional need to reverse a file by lines. Usually, I would use tac, the gnu utility for that purpose, but I wanted to be able to do it with perl in a pinch. Then I found this one-liner:
perl -e 'print reverse <>' file1 file2 file3 ....
This is a *NIX style one-liner, for windows you would type:
perl -e "print reverse <>" file1 file2 file3 ....
I was suspicious of it, and didn't bother to try it for a while. The 'reverse' function is a list operator, and I thought it would load the file into memory to reverse it (for large files this is a big no-no). But, it turns out that reverse has a 'diamond-operator'(the <> above) context, and this contsruct will tell perl to zoom to the end of the file and start reading it in backwards. Very handy.
Outside of a 'one-liner' context, this construct can be used to perform operations on a file like this:
foreach (reverse(<>)) {
do some stuff to the line....
}
I have an occasional need to reverse a file by lines. Usually, I would use tac, the gnu utility for that purpose, but I wanted to be able to do it with perl in a pinch. Then I found this one-liner:
perl -e 'print reverse <>' file1 file2 file3 ....
This is a *NIX style one-liner, for windows you would type:
perl -e "print reverse <>" file1 file2 file3 ....
I was suspicious of it, and didn't bother to try it for a while. The 'reverse' function is a list operator, and I thought it would load the file into memory to reverse it (for large files this is a big no-no). But, it turns out that reverse has a 'diamond-operator'(the <> above) context, and this contsruct will tell perl to zoom to the end of the file and start reading it in backwards. Very handy.
Outside of a 'one-liner' context, this construct can be used to perform operations on a file like this:
foreach (reverse(<>)) {
do some stuff to the line....
}
0 Comments:
Post a Comment
<< Home