Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn linux and unix commands - unix shell scripting

Pass variable to sed?

Shell Programming and Scripting


Tags
sed

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-04-2002   -   Original Discussion by kristy
kristy's Unix or Linux Image
kristy kristy is offline
Registered User
 
Join Date: May 2001
Last Activity: 15 May 2007, 10:18 AM EDT
Location: Allentown, PA
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 04-04-2002   -   Original Discussion by kristy
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 63
Thanked 471 Times in 271 Posts
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
The Following 2 Users Say Thank You to Perderabo For This Useful Post:
chrisjbell (12-13-2012), kalak (11-08-2011)
Sponsored Links
    #3  
Old Unix and Linux 04-04-2002   -   Original Discussion by kristy
kristy's Unix or Linux Image
kristy kristy is offline
Registered User
 
Join Date: May 2001
Last Activity: 15 May 2007, 10:18 AM EDT
Location: Allentown, PA
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Computer

Worked great! Thanks for the tip.
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pass value of pwd as variable in SED to replace variable in a script file cielle Red Hat 6 03-28-2012 03:47 AM
How to pass a function with a variable parameter into another variable? U_C_Dispatj Shell Programming and Scripting 2 05-26-2011 12:44 PM
ksh variable pass to awk meman1188 Shell Programming and Scripting 4 05-10-2009 01:30 AM
How do I pass a variable to awk? eja UNIX for Dummies Questions & Answers 12 04-03-2007 06:46 PM
How to pass a variable to Awk ? Raynon Shell Programming and Scripting 24 02-26-2007 09:25 AM



All times are GMT -4. The time now is 12:33 PM.