Pass variable to sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass variable to sed?
# 1  
Old 04-04-2002
Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed?

The affected portion of my script goes something like this:

Code:
A=`cut -d. -f1 $FILE`
B=`cut -d. -f2 $FILE`
C=`cut -d. -f3 $FILE`

sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile

now, I understand that the single quote prevents the shell from interpreting special characters, but if I omit it, the process just hangs there.

Thanks,
kristy

(working with Solaris)

Last edited by Yogesh Sawant; 10-19-2010 at 08:21 AM.. Reason: added code tags
# 2  
Old 04-04-2002
Re: Pass variable to sed?

Take the variables out of the single quotes...
Code:
sed 's/1111/'${A}'/g;s/2222/'${B}'/g;s/3333/'${C}'/g' file > anotherfile

At least that should always work. For the example you posted, it looks like double quotes would've worked. If the contents of the variables are funky, you may need double quotes around them.

IE change ${A} above to "${A}"

Last edited by Yogesh Sawant; 10-19-2010 at 08:21 AM.. Reason: added code tags
These 2 Users Gave Thanks to Perderabo For This Post:
# 3  
Old 04-04-2002
Computer

Worked great! Thanks for the tip.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

5. Shell Programming and Scripting

Pass variable to SFTP

Hi All, I am using SFTP command to transfer the file. SRC=`pwd` DIG=123 /bin/sftp -B /dev/stdin xyz@abc.net <<:: mput ${SRC}/GGG.${DIG}.tar.gz bye :: The problem is, after entring into sftp command, it is not taking variable values of SRC and DIG. can someone help me... (0 Replies)
Discussion started by: ace_friends22
0 Replies

6. Shell Programming and Scripting

pass variable to sed like in awk (-v switch)

hi all is possible to pass shell (bash) variable to sed like it is in awk? example: awk script is storred in awk.awk file and I am passing variable called var to this file. $ cat awk.awk {if ($5==var) print $0} so it works when i issue $ bash_var=24 $ ls -l | awk -v... (1 Reply)
Discussion started by: wakatana
1 Replies

7. UNIX for Dummies Questions & Answers

How To Pass an Array Variable

Hi, I have a master BASH shell script where I define a bunch of variables: $var1=why $var2=is $var3=(this so hard) I would then like to call another shell script and pass these variables to it: $script2 $var1 $var2 $var3 This works fine for var1 and var2. However, var3 is an array,... (9 Replies)
Discussion started by: msb65
9 Replies

8. Shell Programming and Scripting

Pass perl variable to sed

Hello, I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also... (4 Replies)
Discussion started by: hi_ryo
4 Replies

9. Shell Programming and Scripting

pass a variable to sed p in a loop?

Hi, Thanks for looking,,,, (kornshell) tmp3 is a list of line numbers I want to print the lines from my list code: while read j do echo $j #works fine echo $filename #works fine #sed "'$jp'" "$filename" ... (7 Replies)
Discussion started by: JohnMario
7 Replies

10. Shell Programming and Scripting

how to pass variable to grep?

Hi I have a such conditional: SPAMH="it is SPAM" if grep -q $SPAMH $NMDIR/$mail; then SPAMHFLAG=1 else SPAMHFLAG=0 fi And grep doesn't catch this string, even it exists there. I think it's a problem with passing $SPAMH to grep. I tried... (2 Replies)
Discussion started by: xist
2 Replies
Login or Register to Ask a Question