in reply to Re: Deleting first and last lines of a text file
in thread Deleting first and last TWO(2) lines of a 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;

Replies are listed 'Best First'.
String interpolation
by hippo (Archbishop) on May 16, 2014 at 16:17 UTC

    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.

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

      my $infile = "/home/vmeruga_alt/EXEC_${filedt}.txt";

      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';