👤


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.

sed not taking variable value

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-12-2018
sdosanjh sdosanjh is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 4 May 2018, 9:04 AM EDT
Posts: 158
Thanks: 62
Thanked 2 Times in 2 Posts
sed not taking variable value

Hi All,
Could you please help me, why sed is not able to take variable value when I try to replace using sed.
I want to replace 2nd column (time) and keeping intact others.

Code:
cur="09:30"
CODE="VL"
new="09:35"

sed s/'\(.*def.monitor."${CODE}".qStart.*\)"${cur}"\(.*read\)/\1"${new}"\2'/g $FILE > /tmp/newFile

From
Code:
abcxx.def.monitor.VL.qStart         09:30               read

TO
Code:
abcxx.def.monitor.VL.qStart         09:35               read



Moderator's Comments:
sed not taking variable value Please use CODE tags correctly (i.e. not enclosing a (sub)total text) as required by forum rules!

Last edited by RudiC; 03-12-2018 at 04:35 PM.. Reason: Changed CODE tags.
Sponsored Links
    #2  
Old 03-12-2018
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 17 July 2018, 10:14 PM EDT
Location: Foxborough, MA
Posts: 8,310
Thanks: 257
Thanked 838 Times in 782 Posts
so many quotes - so little time...
Code:
sed "s/\(.*def.monitor.${CODE}.qStart.*\)${cur}\(.*read\)/\1${new}\2/g" $FILE

The Following User Says Thank You to vgersh99 For This Useful Post:
sdosanjh (03-13-2018)
Sponsored Links
    #3  
Old 03-12-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 July 2018, 3:29 PM EDT
Location: Aachen, Germany
Posts: 13,065
Thanks: 449
Thanked 4,013 Times in 3,690 Posts
Quote:
Originally Posted by sdosanjh View Post
Hi All,
Could you please help me, why sed is not able to take variable value when I try to replace using sed.
.
.
.
Wouldn't a better wording be "sed is not taking shell variable value when used in above setup"?
sed by its design / implementation is not meant to take shell variables (there's more suitable tools like awk or perl), and even less so when used like posted in post#1:
- Single quotes prevent shell expansion.
- Single quotes within the parameters might confuse the tool AND users.
- the entire "{script-only-if-no-other-script}" (c.f. man sed on linux Ubuntu 17.10) should be quoted - just to be on the safe side.

Would
Code:
sed "s/\(.*def.monitor.${CODE}.qStart.*\)${cur}\(.*read\)/\1${new}\2/g" file
abcxx.def.monitor.VL.qStart         09:35               read

come close to what you need?
The Following User Says Thank You to RudiC For This Useful Post:
sdosanjh (03-13-2018)
    #4  
Old 03-13-2018
sdosanjh sdosanjh is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 4 May 2018, 9:04 AM EDT
Posts: 158
Thanks: 62
Thanked 2 Times in 2 Posts
Thank you RudiC and Vgersh99. The sed is working as expected.
let me see if I can get the same results using awk.

I am not familiar with awk though. So tried sed therefore because I always need to replace the second column in the file.

Last edited by sdosanjh; 03-13-2018 at 12:26 PM..
Sponsored Links
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with taking variable of for loop in textfile mmaz Shell Programming and Scripting 4 11-08-2017 06:14 AM
How to pass value of pwd as variable in SED to replace variable in a script file cielle Red Hat 6 03-28-2012 02:47 AM
SED taking too much time dashing201 UNIX for Dummies Questions & Answers 3 06-07-2011 08:24 AM
Taking sed result in a variable abhinav192 Shell Programming and Scripting 4 11-25-2009 05:25 PM
taking every variable and executing the command srichunduru Shell Programming and Scripting 6 08-25-2005 01:54 PM



All times are GMT -4. The time now is 10:40 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?