Refrence Shell variable in Perl code.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Refrence Shell variable in Perl code.
# 1  
Old 07-12-2008
Refrence Shell variable in Perl code.

Smilie

I have 2 files a & b
Contents of a are:-
--------------------------
This is a Good Boy
This is a Good Boy
This is a Very Good Boy
This is a Good Boy
This is a Good Boy
---------------------------

The contents of b are
-------------------
This is Testing Beta
--------------

I have a piece of code :-

i=`cat a|grep "Very Good"`
echo $i
perl -pi -e 's/^(This is a Very Good Boy)/$1."\n".`cat b`/e' a

Now the above piece of code works perfectly well. That is String 'This is Testing Beta', needs to be added just under the string ''This is a Very Good Boy', followed by the remaining lines of code from file a. However, I want to refrence Shell variable 'i' instead of hard coding the string 'This is a Very Good Boy'? Please help urgent.

Any other forms of help are also welcome, I mean if we could do the above effort with just Shell,awk,sed or combination of all 3. All your help is highly appreciated.

Regards
Sri
Smilie

The Final Output Should look something like this. I mean the final contents of File a
----------------------
This is a Good Boy
This is a Good Boy
This is a Very Good Boy
This is Testing Beta

This is a Good Boy
This is a Good Boy
-------------------------
# 2  
Old 07-12-2008
Something like that:
Code:
perl -pi -e 's/^($ENV{i})/$1."\n".`cat b`/e' a

# 3  
Old 07-12-2008
Or:

Code:
myVar="This is a Very Good Boy"

sed '/'$myVar'/r b' a

Regards
# 4  
Old 07-12-2008
Something is going on with the way this forum is presented to my PC and it is no longer so easy to find a way to reply...

But you have the answer in one of the other replies already...

the %ENV hash (built into perl) holds all environment vars. Inside your perl script you can use $ENV{i} and that will be the value of the shell variable 'i'
# 5  
Old 07-13-2008
Quote:
Originally Posted by mirusnet
Something like that:
Code:
perl -pi -e 's/^($ENV{i})/$1."\n".`cat b`/e' a

Hi Buddy,

Thanks for the quick reply. But this is not working the way I want. The output using the above piece is

This is Testing Beta
This is a Good Boy
This is Testing Beta
This is a Good Boy
This is Testing Beta
This is a Very Good Boy
This is Testing Beta
This is a Good Boy
This is Testing Beta
This is a Good Boy

Please note that the variable i is a Shell variable, which has the output of Unix Command.
i=`cat a|grep "Very Good"`

Please help buddy its a emergency.
# 6  
Old 07-13-2008
Quote:
Originally Posted by send2sridhar

Please help buddy its a emergency.
This is definitely not encouraged in the forum. Please Smilie

Here is one more,

Code:
awk -v var=$i 'BEGIN { while (getline < "second_file") arr[i++]=$0 } { if(match($0, var)) { for (j=0; j<i ;j++) { printf "%s\n%s\n\n", var, arr[j] }; next } print }' first_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of shell variable in perl command

Hi, I have a shell script, In which i have variable "var1" and some perl command inside shell script. export var1='coep -n rst-a2p-hinje.vci.all.com -c' perl -pi -e 's/^/coep -n rst-a2p-hinje.vci.all.com -c /' command.txt currently I am adding value of var1 in command.txt file by... (2 Replies)
Discussion started by: rakeshtomar82
2 Replies

2. Shell Programming and Scripting

Replace from refrence file

Input file FS01,/root_vdm_9/UKSR06_FS01,test1 FS04,/root_vdm_11/UKSR04_FS04,test2 FS01,/root_vdm_7/UKSR02_FS01,test4 FS01,/root_vdm_12/UKSR03_FS01/dhdhdhdh,test5 FS01,/root_vdm_11/UKSR04_FS01/gggg,test6 Reference File /root_vdm_9/UKSR06_FS01,UKSR06_FS01... (2 Replies)
Discussion started by: greycells
2 Replies

3. Shell Programming and Scripting

Use shell variable in perl command line

Hi, I would like to use a shell variable $amp in my perl command line. for fa in $WORKSPACE/*.fa; do amp=`grep ">.*" $fa | sed -e's#>\(.*\)#\1#g'` ampsam="$WORKSPACE/$base/$base.$amp.sam" sqheader=`grep "^@SQ.*SN:$amp.*" $sam` printf "$sqheader\n" >> $ampsam ... (3 Replies)
Discussion started by: jdilts
3 Replies

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

5. Shell Programming and Scripting

Replace from Refrence file

Need help with this .. Input file 234,6006016061302E006BE89E7FD64DE111,6006016061302E00B824FDA6D32CE111,Complete Refrence File 6006016061302E006BE89E7FD64DE111:vvv_222:433:RP_JRNL_LUN_3005 6006016061302E00B824FDA6D32CE111:vvv_444:344:laputa_LUN_95_L If $2 or $3 from input... (1 Reply)
Discussion started by: greycells
1 Replies

6. Shell Programming and Scripting

Passing perl variable to shell command

Can we pass perl variable to shell commands. If yes, please give some example. (2 Replies)
Discussion started by: Anjan1
2 Replies

7. Shell Programming and Scripting

Assign perl output to ksh shell variable

Hello, I am writing a ksh script on an AIX system. I need to get the date and time from a file into a variable. I found the following perl script from another post on this site and modified it slightly to output the format I need: perl -e '@d=localtime ((stat(shift)));... (4 Replies)
Discussion started by: swimp
4 Replies

8. Shell Programming and Scripting

Converting Perl code to shell

I have a perl code that runs like Code I sub p { if ($d >= $D) {return} printf "%3s"x$d++," "; printf "%s%s\n",$_,$h{$_}?" ** ":""; if (!$h{$_}) { $h{$_}=1; map {p($_)} @{$s{$_}} } $d-- } ($Set,$Job,$Num,$D) = (@ARGV); map {shift} 0..3; (8 Replies)
Discussion started by: zainravi
8 Replies

9. Shell Programming and Scripting

Perl script variable to read shell command

Solaris 10 Korn shell ksh, Hi there, I have figured out to get yesterday's date which is using the below command: TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and TZ=GMT+24; date +%Y%m%d to get the format of 20080930. I need this two format. In my perl script below I need... (4 Replies)
Discussion started by: bulkbiz
4 Replies

10. Shell Programming and Scripting

Perl: export variable to shell

Hi, I am running a series of scripts and I need to transport a particular variable across many scripts. I thougt of defining an environmental variable which I could access through. But I found that the variable dies as soon as the script ends.. Currently I write this variable to a temporary... (2 Replies)
Discussion started by: oldtrash
2 Replies
Login or Register to Ask a Question