Replacing variable value in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing variable value in perl script
# 1  
Old 03-25-2010
Question Replacing variable value in perl script

hi I have this line in my perl script

Code:
 
if (($cookie =~ /^cookie(.*)\/(.*)/) && ($ !~ /^cookie(10)\/(.*)|/)) {
                        $cookienumber = "$1.$2";
                        }

now if the result is cookie1/0,

my $cookienumber would be 1.0

but if the result is cookie1/0/0

my $cookienumber would be 1/0.0

how can I replace the variable for result 1/0.0 to 1.0.0?

How can I change $1 value from 1/0 to 1.0?

Please help thanks

---------- Post updated at 05:55 PM ---------- Previous update was at 05:30 PM ----------

Quote:
Originally Posted by tententen
hi I have this line in my perl script

Code:
 
if (($cookie =~ /^cookie(.*)\/(.*)/) && ($ !~ /^cookie(10)\/(.*)|/)) {
                        $cookienumber = "$1.$2";
                        }

now if the result is cookie1/0,

my $cookienumber would be 1.0

but if the result is cookie1/0/0

my $cookienumber would be 1/0.0

how can I replace the variable for result 1/0.0 to 1.0.0?

How can I change $1 value from 1/0 to 1.0?

Please help thanks
# 2  
Old 03-25-2010
Not sure I fully understand, but are you looking for something like this ?

Code:
$
$ cat -n cookies
     1  cookie1/0
     2  cookie1/0/0
     3  cookie1/0/0/0
     4  cookie1/0/0/0/0
     5  notacookie1/0/0
$
$ perl -lne '/^cookie/ && s!/!.!g; print' cookies
cookie1.0
cookie1.0.0
cookie1.0.0.0
cookie1.0.0.0.0
notacookie1/0/0
$
$

tyler_durden
# 3  
Old 03-25-2010
Question

change of plan :P

I need help with the regular expressions instead as I have tweaked my script

if $cookie =~ /^cookie(.)\/(.)\/(.)/

$cookietype="$1.$2.$3"

so if my $cookie is cookie1/0/0

my $cookietype will be 1.0.0

Problem is, if my $cookie is cookie1/0,

it doesnt match any of the pattern so it'll set the value to 0 by default
(defined in my final else statement).

I added elsif statement as below

elsif $cookie =~ /^cookie(.)\/(.)/
$cookietype="$1.$2"

and the value became 0, as if it doesnt match the pattern Smilie

please help.
# 4  
Old 03-25-2010
Code:
$
$
$ cat -n cookies
     1  cookie1/0
     2  cookie1/0/0
     3  cookie1/0/0/0
     4  cookie1/0/0/0/0
     5  notacookie1/0/0
$
$
$ perl -lne 'chomp($cookie=$_);
>            if ($cookie =~ /^cookie(.)\/(.)\/(.)$/) {$cookietype = "$1.$2.$3"}
>            elsif ($cookie =~ /^cookie(.)\/(.)$/) {$cookietype = "$1.$2"}
>            else {$cookietype = "0"}
>            print $cookietype' cookies
1.0
1.0.0
0
0
0
$
$

tyler_durden
# 5  
Old 03-25-2010
it worked!, thank you so much! Smilie
# 6  
Old 03-25-2010
Glad to know that!

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. 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

3. Shell Programming and Scripting

Using variable from shell script in perl file

Hey, So I have a shell script that outputs some variables, call them $a and $b. I know in shell scripting if I wanted to use the variables in another shell script I'd do sh code.sh "$a" "$b" How can I do something similar with perl? (2 Replies)
Discussion started by: viored
2 Replies

4. Shell Programming and Scripting

Replacing strings in perl script

HI all, These are examples of the original value from a variable $abc can be FastEthernet1/0 GigabitEthernet3/1 Serial1/0 If $abc is FastEthernet*/* (where * can be any number), replace $abc value to fa*/* (same number as the original value). GigabitEthernet becomes ga*/* and Serial... (2 Replies)
Discussion started by: tententen
2 Replies

5. Shell Programming and Scripting

Perl Variable Check Script

I am working on a perl script that is used to update a list of hosts to a certain file but I am having an issue when I try to perform a check to make sure the user enters valid information. The following is what I have currently written for the script: IPINPUT: print "Enter IP Address: ";... (2 Replies)
Discussion started by: Takau
2 Replies

6. Shell Programming and Scripting

Increment a date variable in perl script

Hi, I have a perl script which prints epoch value of date in milliseconds as the output. My reuirement is that once the output is printed,the day variable shld increment by 1 and when i execute the script for the second time the output shld be for the new day value. My script looks as... (11 Replies)
Discussion started by: jyothi_wipro
11 Replies

7. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies

8. Shell Programming and Scripting

visibility of a variable in Perl script.

I am writing a script to cross check the dbscript. For that I am searching the SQL manipulators in the dbscript as shown below. But my problem is the variable $pattern is coming as null when comes out of the foreach loop. File content: ========= vi /home2/niroj_p/dbscript.sql -------... (1 Reply)
Discussion started by: Niroj
1 Replies

9. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies

10. Shell Programming and Scripting

Perl script variable passed to Oracle query

Hi All, I pass a Perl script variable, whch is passed to a query to be prepared. But the problem is I have special character like '&' in this variable which are handled in a special way by the Oracle query parser. How do I get over this? my $cust_name='A&B'; my $sql="Select cust_short_name... (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question