PERL script -- calling 'sed' by passing 'variable value'.

Hi Friends,

I'm calling 'sed' command inside one perl script, which is to list directory names which are having some date value as their names (in the form YYYYMMDD) with in the range (start and end date).

#!/usr/bin/perl -w
use strict;
use warnings;

my $DATA = "/export/home/ganapa";
my $report_start_date   = $ARGV[0];
my $report_end_date     = (scalar(@ARGV) == 1) ? $ARGV[0] : $ARGV[1];

system ("echo '$report_start_date'");
system ("echo '$report_end_date'");

my @selected_rundates  = system ("ls -1 $DATA | sed -n \"\/20101101\/,\/20101210\/p\"");
print "@selected_rundates\n";

The above script is working exactly as it should. This code is selecting and printing date named directories between the selected range.

But if I pass variable names, instead of hard coded values (20101101 and 20101210) along with 'sed' command, script will not print any thing !!

In the below line, I'm just replacing hard coded dates with date variables.
But, it is not printing anything!

my @selected_rundates = system ("ls -1 $RWOUTPUT | sed -n \"\/$report_start_date\/,\/$report_end_date\/p\"");

I tried all possibilities, but couldn't able to figure out the problem with the above piece of code.:confused:

Could any one help me in this regard please?

With Thanks and Regards,
Mysore 101 Ganapati.

Hi
Enclose the variable in the sed command with single quote.

sed -n \"\/'$report_start_date'\/,\/'$report_end_date'\/p\""

Guru.

my @selected_rundates = system ("ls -1 $RWOUTPUT | sed -n \"\/'$report_start_date'\/,\/'$report_end_date'\/p\"");

or

my @selected_rundates = system ("ls -1 $RWOUTPUT | sed -n \"\/\"$report_start_date\"\/,\/\"$report_end_date\"\/p\"");

I tried enclosing with both single quotes as well as double quotes, but no luck :confused:
Thats what surprised me and hence posted here :wink:

Just one question: why write a Perl "script", if all you're doing is shell scripting? This could be re-written just as well as

#!/bin/sh

DATA="/export/home/ganapa"
report_start_date=$1
report_end_date=${2:-$1}

echo $report_start_date
echo $report_end_date

ls -1 $DATA | sed -n "/$report_start_date/,/$report_end_date/p"

Or, using pure Perl:

#!/usr/bin/perl -w

use strict;
use warnings;

my $DATA              = "/export/home/ganapa";
my $report_start_date = $ARGV[0];
my $report_end_date   = ( defined $ARGV[1] ? $ARGV[1] : $ARGV[0] );

print "$report_start_date\n";
print "$report_end_date\n";

opendir my $dir, $DATA || die "Can't open $DATA for reading: $!";
foreach my $entry ( sort readdir($dir) ) {
    local $_=$entry;
    print $entry, "\n" if /$report_start_date/ .. /$report_end_date/;
}
closedir $dir;
1 Like

Thanks a lot Pludi :b:
Your solution worked.

The requirement was in perl, but I know only little on perl and bit familiar in shell when compared to perl. Hence I've mixed it up according to my little knowledge.

I will work more on perl in future, to get more knowledge but struggling with Hashes (which scares me a lot with its concepts and hash references).

Thanks again :slight_smile:

U can try this also.

my $cmd = "ls -1 $RWOUTPUT | sed -n \"\/$report_start_date\/,\/$report_end_date\/p\"";
my @selected_rundates = system ("$cmd");