sed substitution and shell variables

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed substitution and shell variables
# 1  
Old 06-11-2011
sed substitution and shell variables


Am trying to substitute the value of a shell variable with the value of another shell variable. The values are obtained into the shell variables through some other processing.
for ex.

now i want to replace "regno" with "usn" in a file
i've used the follow sed command..
sed "s/$var1/$var2/g"

i've also tried the other alternatives i found searching the threads. But nothing worked.. .
Please help!
# 2  
Old 06-11-2011
did you try this ?

sed -i '' "s/${var1}/${var2}/g" yourfile

# 3  
Old 06-11-2011
I had not tried it earlier, but now i did..

the error am finding is
sed: can't read s///g: No such file or directory

# 4  
Old 06-11-2011
did you specify your filename at the end of the command ?
did you pake sure you didn't forget any simplequote nor double quote ?

for me it works :

$ cat tst
Frinto Francis
Frinto cool
change Attitude
$ var1=cool
$ var2=CEWL
$ sed -i '' "s/${var1}/${var2}/g" tst
$ cat tst
Frinto Francis
Frinto CEWL
change Attitude

# 5  
Old 06-11-2011
i've checked every detail of the command and specified it in the same manner..

the error this time is
sed: can't read s/cool/CEWL/g: No such file or directory

# 6  
Old 06-11-2011
Originally Posted by ctsgnb
sed -i '' "s/${var1}/${var2}/g" yourfile

the problem is probably the two single quotes after "-i". If i remember correctly "-i" means "change the file in place, instead of writing the results of the substitions to another file". If this is the case the '' is treated as the regex expression and the regex is treated as filename.

It is not a good idea to use the "-i" switch anyways, because firstly you run the risk of changing your original with some unwanted substitution and secondly the "-i"-switch is not supported by most sed-versions and hence your command is not very portable.

My suggestion is to write it this way:

sed "s/${var1}/${var2}/g" /path/to/yourfile | more

Once you are satisfied with the outcome displayed you can replace "| more" by some redirection to a file. Use "mv" to overwrite the original with this file if you want to do so.

sed: can't read s///g: No such file or directory

This looks like your variables aren't being expanded at all. Check the spelling of your variables (even capitalization counts, "$a" and "$A" is NOT the same).

A second possible problem is variable scope: Maybe the variables are not set in the environment you (try to) use them but another. Try this to make sure this is not the problem:

var1="<...>"; var2="<...>" ; sed "s/${var1}/${var2}/g" /path/to/yourfile | more

I hope this helps.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with substitution between two variables

To all geeks, What I want to achieve: 1. Accept two filenames from user and store the filenames in two variables (FILE1 and FILE2) 2. Check if files exisits. If doesn't, then exit 3. If files exist, look for a particular string in both files 4. If the string exists, then change the... (8 Replies)
Discussion started by: Deepak Tulsani
8 Replies

2. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

3. Shell Programming and Scripting

Substitution with sed

I have a file with some numbers having single quotes around them which I want to remove. i.e. '923930' -> 23930 If it can be done without using sed thats fine. I have tried with sed but can't think how to replace this pattern on only the numbers (13 Replies)
Discussion started by: user_invalid
13 Replies

4. Shell Programming and Scripting

sed command using variables in shell script

hi guys, The following command doesn't seem to work in my shell script: tag=$(sed -n '/${line}/ s/.*\.*/\1/p' myfile.txt) When i replace the ${line} with an actual value, it works fine. So, how do i use the ${line} in this sed command? Thanks in advance, Zaff (2 Replies)
Discussion started by: zaff
2 Replies

5. Shell Programming and Scripting

[bash] command line substitution with environmental variables

Hi, I'm using an array that contains compiler FLAGS that need to be executed either before ./configure or after the main 'make' command. example of array containing compiler flags. ------------------------------------------------- FLAGS="CFLAGS=\"-arch x86_64 -g -Os -pipe... (7 Replies)
Discussion started by: ASGR
7 Replies

6. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

7. Shell Programming and Scripting

SED Substitution

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as well to... (1 Reply)
Discussion started by: shubhranshu
1 Replies

8. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

9. UNIX for Advanced & Expert Users

shell variables and sed substitution

My specific goal: automatically edit a Makefile variable's (EXTRAVERSION) value in a kernel Makefile. So, I have a shell script that takes one parameter, a version string: In the script, I assign the parameter to K_VER: K_VER=""... (2 Replies)
Discussion started by: duderonomy
2 Replies

10. Shell Programming and Scripting

Double Substitution variables in ksh

Hi I have a variable whose value is like this i=/test/test1/test2/ i would like to have the value of stored into another variable my attempt is test=${i##*/} ;echo $test ##and i get since what i wnat is i try this... (19 Replies)
Discussion started by: xiamin
19 Replies
Login or Register to Ask a Question