Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Deleting first and last TWO(2) lines of a text file

by vsmeruga (Acolyte)
on May 16, 2014 at 09:20 UTC ( [id://1086260]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Gurus I am an architect and not into any scripting but I have a task to remove first and last lines of text file. Can someone help me with the code or link to the code Many Thanks VJ

  • Comment on Deleting first and last TWO(2) lines of a text file

Replies are listed 'Best First'.
Re: Deleting first and last lines of a text file
by morgon (Priest) on May 16, 2014 at 09:32 UTC
    perl -i.old -ne 'print unless $.==1 or eof' <input-file>
    This will also keep the old version around as <input.file>.old

      Hi I tried the code like this and got error as

      syntax error at ./perl_deletelines.pl line 5, near "-ne" Execution of ./perl_deletelines.pl aborted due to compilation errors. </P

      #!/usr/bin/perl use strict; use warnings; perl -i.old -ne 'print unless $.==1 or eof' /home/file_20140407.txt

        The script given to you by morgon is a oneliner, which you should "run" from your command line interface, not from your a script.
        If you want a full script of the oneliner this would do:

        BEGIN { $^I = ".old"; } LINE: while ( defined( $_ = <ARGV> ) ) { print $_ unless $. == 1 or eof; }
        putting the above in a script should work.

        Of course, morgon code works fine from the CLI.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
      Thanks for the reply. I will try and let you know
Re: Deleting first and last lines of a text file
by AppleFritter (Vicar) on May 16, 2014 at 09:32 UTC

    Something like this should work:

    #!/usr/bin/perl <>; while($line = <>) { print if defined; $_ = $line; }

    Explanation: the angle bracket operator reads a line from a while, so the first line is read and discarded. After that, $_ (Perl's implicit topic variable) is used to store the previous line; it's initially undefined, so the print statement doesn't get executed on the first loop iteration, only the subsequent ones. Since only the previous line gets printed, not the current line, the last line of the file is skipped.

    EDIT: here's a slightly more idiomatic solution:

    #!/usr/bin/perl <>; while(<>) { last if eof; print; }

    It's pretty much the same as before, but does away with remembering the previous line and instead just exits the loop before printing if the end of the file has been reached, thus neglecting to print the last line.

    EDIT 2: just so there's no confusion in the future, the OP originally asked for the first and last line of a text file to be removed, not the first line and last two lines.

      Hi I tried with below code calling scripts as ./scriptname srcfile.txt. But it deleted just the first line. Not last 2 lines/1 line </p?

      #!/usr/bin/perl my $file='jjl.exec.txt'; open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; <>; while($line = <>) { print if defined; $_ = $line; }

        Hmm, that should... and is, in fact, working for me. Are you sure there's not simply an empty line at the end of your input file? Perhaps you could share some sample data.

        Also, why the open calls? If you want the output to go to a specific file, it'd be easier to simply redirect the script's output on the command line, like so:

        $ ./scriptname srcfile.txt >jjl.exec.txt

        Alternatively, I'd suggest at least using a different filehandle than STDOUT, though you'd of course have to adjust the print statement to print to that filehandle then. opening STDOUT like that apparently works, but it's giving me the heebies.

      Hi Where are we specifying the filename here?. Thanks VJ

        You can either have the script read from STDIN, or supply a filename as an argument, i.e.:

        $ perl script.pl data.dat

        or

        $ cat data.dat | perl script.pl

        Such is the magic of the angle bracket operator when used with a null filehandle. Quoting perlop:

        The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop

        while (<>) { ... # code for each line }

        is equivalent to the following Perl-like pseudo code:

        unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

        except that it isn't so cumbersome to say, and will actually work. It really does shift the @ARGV array and put the current filename into the $ARGV variable. It also uses filehandle ARGV internally. <> is just a synonym for <ARGV>, which is magical. (The pseudo code above doesn't work because it treats <ARGV> as non-magical.)

        Hope this helps!

Re: Deleting first and last lines of a text file
by ww (Archbishop) on May 16, 2014 at 11:09 UTC

    Hi, vsmeruga. I'm a coder, not an architect, but I need to design and build a skyscraper.

    If I ask how to do each step and question each bit of advice I'm given (without referring to the tutorials and examples here), will you build me my skyscraper step-by-step and without compensation?



    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Gurus I know programming but not Perl. That is why I have asked you for help. Many thanks VJ
Re: Deleting first and last lines of a text file
by blue_cowdawg (Monsignor) on May 16, 2014 at 13:56 UTC

    #!/usr/bin/perl -w use Tie::File; use strict; my $fname = shift @ARGV; tie my @file,'Tie::File',$fname or die $!; pop @file; pop @file; shift @file; untie @file; exit(0);


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Deleting first and last lines of a text file
by DrHyde (Prior) on May 16, 2014 at 10:48 UTC
    HEAD=`head -1 myfile.txt` TAIL=`tail -1 myfile.txt` grep -v $HEAD myfile.txt|grep -v $TAIL>myfile.txt~ mv myfile.txt~ myfile.txt

    Untested code. Needs some debugging.

      That'll not just delete the first and the last line, but also any line that happens to be the same. I.e., it'll turn this:

      the quick brown fox jumps over the lazy dog's back

      into this:

      quick brown fox jumps over lazy dog's

      rather than this:

      quick brown fox jumps over the lazy dog's

      Hi Gurus I just checked the file is having another extra line in the last "^Z" character. so I should remove last 2 lines and first line sorry for the correction. please help! Thanks VJ

        This deletes the fist line and the last 2 lines of input.txt and keeps a copy of the old version in input.txt.old.
        perl -i.old -ne 'my $l = <>; print($l), next if $.==2; print "$_$l" un +less eof' input.txt
        Note that this is a one-liner to be run as is from the command-line (adjust your input file-name).
Re: Deleting first and last lines of a text file
by hippo (Bishop) on May 16, 2014 at 13:51 UTC

    Here is a relatively simple generic solution to be tailored to your specific requirements. I've named it headtail.pl but you can call it whatever you want.

    #!/usr/bin/perl -Tw use strict; use warnings; my $skiphead = 1; my $skipfoot = 2; my $infile = 'test.txt'; my $outfile = '/tmp/out.txt'; my @buffer = (); open IN, '<', $infile or die "Cannot open $infile for reading: $!"; open OUT, '>', $outfile or die "Cannot open $outfile for writing: $!"; <IN> for (1..$skiphead); push @buffer, scalar <IN> for (1..$skipfoot); while (<IN>) { print OUT shift @buffer; push @buffer, $_; } close OUT; close IN;
      Oh! YES. Thank you very much. It works.. deleted first line and last 2 lines in my text file

      Please suggest... Hi Now I am trying to replace a date variable in the filename. It prints the date perfectly when I do it separately in test script. But That date variable; ${filedt}.(YYYYMMDD) format doesn't working in this code

      #!/usr/bin/perl use Time::Piece; use strict; use warnings; my $filedt = localtime->strftime('%Y%m%d'); my $skiphead = 1; my $skipfoot = 2; my $infile = '/home/vmeruga_alt/EXEC_${filedt}.txt'; my $outfile = '/home/vmeruga_alt/EXEC_tgt.txt'; my @buffer = (); open IN, '<', $infile or die "Cannot open $infile for reading: $!"; open OUT, '>', $outfile or die "Cannot open $outfile for writing: $!"; <IN> for (1..$skiphead); push @buffer, scalar <IN> for (1..$skipfoot); while (<IN>) { print OUT shift @buffer; push @buffer, $_; } close OUT; close IN;

        Have a read about quoting to see why the variable isn't interpolated. Also, never say "it didn't work" as a bug report - that's pretty much content-free.

      Error Message I get: Cannot open /home/vmeruga_alt/EXEC_${filedt}.txt for reading: No such file or directory at ./perl_deletelines_jjl.pl line 17.

Re: Deleting first and last lines of a text file
by Anonymous Monk on May 16, 2014 at 09:21 UTC
Re: Deleting first and last lines of a text file
by Bloodnok (Vicar) on May 16, 2014 at 10:25 UTC
    sed -e '1d;$d' < infile > outfile

    A user level that continues to overstate my experience :-))
      Hi I tried the below shell code. But it removed only first line. Thanks
      #!/bin/sh sed -e '1d;$d' /home/file_20140407.txt >>file_20140407_n.txt
        Works for me ...
        $ sed -e '1d;$d' jaxws/src/share/jaxws_classes/com/sun/xml/internal/me +ssaging/saaj/soap/SOAPDocumentFragment.java > t $ diff jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/sa +aj/soap/SOAPDocumentFragment.java t 1d0 < /* 45d43 < }
        Does your file have a newline as the last character ? You'll experience problems if not.

        A user level that continues to overstate my experience :-))
Re: Deleting first and last lines of a text file
by admiral_grinder (Pilgrim) on May 16, 2014 at 13:39 UTC

    Today's coding warm-up using Path::Class

    use Path::Class qw{ file }; # # Getting file name is left to reader # my $input_file = file( $filename ); # # WARNING: Destructive operation! # my $output_file = $input_file; # Left to the reader to change my @file_contents = $input_file->slurp(); shift @file_contents; # Remove first line pop @file_contents; # Remove last line $output_file->spew( @file_contents );
Re: Deleting first and last lines of a text file
by Anonymous Monk on May 16, 2014 at 09:30 UTC
    # In file: "command.ed" 1 d $ d w Q # ed file-to-modify < command.ed >/dev/null

      Above was display of lack of my ed-fu; after light "man ed" ...

      # In file: "command.ed" 1d $d wQ ...
        Hi I have to automate the task as I have to delete lines in the file every day before my original load task begins Thanks VJ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-20 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found