sed not taking variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed not taking variable value
# 1  
Old 03-12-2018
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:
Mod Comment 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 05:35 PM.. Reason: Changed CODE tags.
# 2  
Old 03-12-2018
so many quotes - so little time...
Code:
sed "s/\(.*def.monitor.${CODE}.qStart.*\)${cur}\(.*read\)/\1${new}\2/g" $FILE

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-12-2018
Quote:
Originally Posted by sdosanjh
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?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 03-13-2018
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 01:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with taking variable of for loop in textfile

Hi, I am new to unix/linux scripting. I have a text file, listlib.txt where the content: lib1_23 lib34_a ab_li_lab I need to generate a file (.log) of each cell. I am planning to create a (.csh) script that will have for loop with variable taken from listlib.txt. As for now, i have no... (4 Replies)
Discussion started by: mmaz
4 Replies

2. Shell Programming and Scripting

How to use the variable in sed?

version=git release=r8 echo lp-testsuite-git-r8.x86_64.rpm |sed -e "s/-$version-$release.*//g" I want to get lp-testsuite what's wrong with me ? (1 Reply)
Discussion started by: yanglei_fage
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. UNIX for Dummies Questions & Answers

SED taking too much time

Hi I am trying to remove some characters from my data file. The data file has huge number of records say 90000 records. I am using sed for this purpose. eg : cat FILENAME|sed 's/;//g' (to remove semi colon ';') However as the data file is too huge , it is taking too much time to give... (3 Replies)
Discussion started by: dashing201
3 Replies

5. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

6. Shell Programming and Scripting

Taking sed result in a variable

Hi All, How can i take the following code having three seds into a variable : echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/+/g' | sed 's/\#/'$job_date'/g' I want to get the result stored in a script variable i tried var2=`echo "$DateFileFormat" | sed 's/\./\\\\./g' |... (4 Replies)
Discussion started by: abhinav192
4 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

using variable in sed

i need to use a value in the Variable to print a particular line from a file using sed command. i tried the below one but its is not working sed -n ' "$var"p ' abc.txt but its is not working please help me to sort out this. (3 Replies)
Discussion started by: Kochu77
3 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

taking every variable and executing the command

Hi, I am trying to export some 50 tables and i want to write a loop and execute the script for every table. I did for one table and its running. Can any one help me for setting a loop and running the script for all the tables thanks (6 Replies)
Discussion started by: srichunduru
6 Replies
Login or Register to Ask a Question