Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Deleting first and last lines of a text file

by hippo (Deacon)
on May 16, 2014 at 13:51 UTC ( #1086313=note: print w/ replies, xml ) Need Help??


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

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;
$ cat test.txt 1 hello 2 there 3 this 4 is 5 a 6 good 7 test $ ./headtail.pl $ cat out.txt 2 there 3 this 4 is 5 a


Comment on Re: Deleting first and last lines of a text file
Select or Download Code
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 14:02 UTC
    Oh! YES. Thank you very much. It works.. deleted first line and last 2 lines in my text file
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 16:08 UTC

    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.

        Below is the code for test.pl and it printed the variable to a output file:test.txt. But it could not replace with the variable(${filedt} in file name)

        #!/usr/bin/perl use Time::Piece; use warnings; use strict; my $file='test.txt'; open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; my $filedt = localtime->strftime('%Y%m%d'); print $filedt; #my $infile = '/home/vmeruga_alt/EXEC_${filedt}.txt';

        got it . Double quotes instead of single here . Thanks!!

        my $infile = "/home/vmeruga_alt/EXEC_${filedt}.txt";
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 16:14 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086313]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-08-20 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (121 votes), past polls