See perlrun (or perldoc perlrun) - This switch can be used to modify the input and output line separators, $/ and $\ respectively. The main use for this type of assignment is in the command line for one-liner scripts used for file processing.
An example modified from perlrun, to trim lines in *.txt files to a maximum width of 80 characters ...
perl -lpe 'substr($_, (length $_ > 79) ? 80 : length $_) = ""' *.txt
This code will step through the file, delete all characters beyond the 80 character length of the string and then write the file back out, retaining the original line separator.
Update : Corrected code example to handle lines less than 80 characters in length - Thanks ariels++
| [reply] [d/l] [select] |
I often use it for one-liners. See perlrun for an example involving (GNU) find. The other example is somewhat broken, unfortunately.
Another example: print second column of file if first starts with an `X'...
cgn_perl -lane 'print $F[1] if $F[0] =~ /^X/'
Well, typing an extra \n isn't that bad, but...
| [reply] [d/l] |
just have a look at my signature :-) There, -l stands for adding a newline at the end. I especially use -l for oneliners. If I use it in real programs, I have to to write some comments about why using it, so I hardly ever use it there.
Best regards,
perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print" | [reply] |