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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL script -- calling 'sed' by passing 'variable value'.
# 1  
Old 12-13-2010
Error 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).

Code:
#!/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!

Code:
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.Smilie

Could any one help me in this regard please?

With Thanks and Regards,
Mysore 101 Ganapati.

Last edited by vbe; 12-13-2010 at 09:46 AM..
# 2  
Old 12-13-2010
Hi
Enclose the variable in the sed command with single quote.


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

Guru.
# 3  
Old 12-13-2010
PHP Code:
my @selected_rundates system ("ls -1 $RWOUTPUT | sed -n \"\/'$report_start_date'\/,\/'$report_end_date'\/p\""); 
or

PHP Code:
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 Smilie
Thats what surprised me and hence posted here Smilie
# 4  
Old 12-13-2010
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
Code:
#!/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:
Code:
#!/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;

This User Gave Thanks to pludi For This Post:
# 5  
Old 12-14-2010
MySQL It worked, Thanks.

Thanks a lot Pludi Smilie
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 Smilie
# 6  
Old 12-14-2010
U can try this also.


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

R0H0N
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable from bash to perl script

Hi All, I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving error : Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50. Can ... (6 Replies)
Discussion started by: NileshJ
6 Replies

2. UNIX for Dummies Questions & Answers

Passing value of a variable in sed

Hi, I want to pass value of a variable track_line which is the line number to sed. Sed should print the lines starting from track_line till the last line of the file. I tried the below command but it is not working. sed -n '${track_line},$p' latest_log_file I tried using the below too but... (1 Reply)
Discussion started by: nitinupadhyaya8
1 Replies

3. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

4. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

5. Shell Programming and Scripting

Passing a variable to sed or cut

:confused: Is it possible to send a variable to a sed or cut command? I have a test script as below: counter=1 while read line do # Test the file printf "$line" > temp$counter pref=$(cut c1-2000 $temp$counter | sed 's///g' | sed 's|.*PutTime\(.*)Origin.*|\1|') printf" let... (5 Replies)
Discussion started by: gugs
5 Replies

6. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

7. Shell Programming and Scripting

Passing a parameter while calling a script

Hello everyone I was asked to pass a parameter in the calling of a script. IŽll have two parameters London and Birmingham and IŽll need to pass this because each of these will have a specific script however with the same name. /home/script/loaddata.ksh Lond /home/script/loaddata.ksh Birm... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

8. Shell Programming and Scripting

Passing exit code to calling script

In production I need to pass an exit code from a script that is being called 3 or 4 layers deep. I've created test scripts to play with it until I get it right. As you can tell, this is not my normal job. Perhaps I should have entered this in UNIX for Dummies section. Anyway, I keep losing the... (2 Replies)
Discussion started by: debbiekuch
2 Replies

9. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies

10. Shell Programming and Scripting

Passing variable to perl

I need a non-perl (bash) way to strip the path from a list of "find" results. Below is the perl version which I could use, if I could figure out how to call the script with a variable (like in sh, $1 is the variable passed in ./script variable) $file = "/path/to/file.txt"; # How do I... (2 Replies)
Discussion started by: TheCrunge
2 Replies
Login or Register to Ask a Question