Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Remove the first word for each lines from a text file

by ArifS (Beadle)
on Dec 22, 2016 at 20:35 UTC ( [id://1178386]=perlquestion: print w/replies, xml ) Need Help??

ArifS has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to Remove the first word for each lines from a text file. Here is the code I am trying with-
use strict; use warnings; open(my $in, '<', 'input.txt') or die "Cannot open input.txt: $!"; open(my $out, '>', 'output.txt') or die "Cannot open output.txt: $!"; while (<$in>) { print $out $_ while s/^\s*\S+\s*//; } close($out); close($in);
input.txt
The person who asked. can mark one answer. as "accepted". Accepting doesn't mean. it's the best answer, it just means that it. worked for the person. who asked.
however output.txt shows-
person who asked. who asked. asked. mark one answer. one answer. answer. "accepted". doesn't mean. mean. the best answer, best answer, answer, just means that it. means that it. that it. it. for the person. the person. person. asked.
I just want to remove the first word for each lines, but it's looping. Any suggestion?

Replies are listed 'Best First'.
Re: Remove the first word for each lines from a text file
by Corion (Patriarch) on Dec 22, 2016 at 20:43 UTC

    You have two loops in your code.

    Remove the one that you don't want.

    If you can't find the inner loop, please explain in plain English what every line of your code is supposed to do and write that as a comment beside each line.

    If a line does more than one thing, consider breaking that line into two lines.

    Consider adding print statements after every line to see what your program is doing and how it progresses.

      ah.... that helps.
      while (<$in>) { $_ =~ s/^\s*\S+\s*//; print $out $_; }
      Working now. Thanks
Re: Remove the first word for each lines from a text file
by Marshall (Canon) on Dec 22, 2016 at 21:06 UTC
    Looks like homework. It is better to admit that, if true. You will still get help and perhaps a more detailed explanation. Since this is Holiday Season..
    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { s/^\s*\S+\s+//; print; } =Output person who asked. mark one answer. "accepted". doesn't mean. the best answer, just means that it. for the person. asked. =cut __DATA__ The person who asked. can mark one answer. as "accepted". Accepting doesn't mean. it's the best answer, it just means that it. worked for the person. who asked.
Re: Remove the first word for each lines from a text file
by talexb (Chancellor) on Dec 22, 2016 at 20:44 UTC

    Think about how you would '.. remove the first word' from a line using Perl. Then do that on each input line, and print the result to the output file.

    You can do it using a regular expression (in fact, there are probably several different ways), but you could also split the line up into words, then re-build the line using all but the first word.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Remove the first word for each lines from a text file -- oneliner explained
by Discipulus (Canon) on Dec 23, 2016 at 08:49 UTC
    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.

      ++ 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

Re: Remove the first word for each lines from a text file
by ExReg (Priest) on Dec 22, 2016 at 21:05 UTC

    Expanding on what Corion said, you second while loop is what is causing the problem. Instead of

    print $out $_ while s/^\s*\S+\s*//;

    get rid of the inner while:

    s/^\s*\S+\s*//; print $out;
      s/^\s*\S+\s*//; print $out;

      Not quite, that prints the value of $out ("GLOB(0xabc...)") to the currently selected filehandle; the print $out $_; is necessary in this case.

        You are right. I blame it on my computer. It is out to get me toda

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1178386]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-29 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found