in reply to Remove the first word for each lines from a text file
All you need to process one or more files at once is the following oneliner (caution to windows doublequotes):
perl -lane "shift @F;print qq(@F)" filename.ext
I suggest you to read perlrun where the four Perl switches -l -a -n -e are explained: in breif -l do the rigth thing with line ending, -a does autosplit of the current line filling the special variable @F (F for fields) (see perlvar too), -n wrap the whole code into a while loop without printing each line (as opposed to -p that prints) and finally -e executes the perl code provided.
You can use the core module Deparse to see the oneliner exploded (see also B::Deparse vs. O=Deparse). I added some,I hope useful, comments:
perl -MO=Deparse -lane "shift @F;print qq(@F)" # the BEGIN block is executed as soon as possible,so is not repe +ted at every iteration over file's lines # input and output record separator set by -l see again perlvar BEGIN { $/ = "\n"; $\ = "\n"; } # the while loop created by -n (do not consider the LINE: label +for the moment) LINE: while (defined($_ = <ARGV>)) { # remove newline at the end of current string (again provided by + -l) chomp $_; # autosplit provided by -a our(@F) = split(' ', $_, 0); # our program starts here shift @F; print "@F"; } -e syntax OK
HtH
L*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Remove the first word for each lines from a text file -- oneliner explained
by kcott (Archbishop) on Dec 23, 2016 at 14:13 UTC |