in reply to Remove the first word for each lines from a text file

Ok you get the job done and got useful suggestions. Now if you want to learn more, Perl can accomplish such simple tasks in few keystrokes.

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*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

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

    ++ for the explanation of the switches.

    Just an observation: you can do that without the shift operation by printing the array slice @F[1..$#F].

    $ perl -lane 'print "@F[1..$#F]"' The person who asked. person who asked. can mark one answer. mark one answer. as "accepted". "accepted". ^C $

    — Ken