Replace value of a variable in a file through script using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace value of a variable in a file through script using sed
# 1  
Old 07-08-2012
Replace value of a variable in a file through script using sed

Hi,

I am trying to replace the value of a variable in a file through another script.

Example:
Filename : abc.txt contents:
a=10
b=20
c=30

Need to change the value of, say, b - Tried using the following:
Code:
sed "s/${b}/15/g" abc.txt

Have tried various forms of sed (with single quotes, without quotes, with -e option, etc) but getting the same error with all:
Code:
sed: -e expression #1, char 0: no previous regular expression

Smilie

Thanks for any help in advance Smilie
# 2  
Old 07-08-2012
Quote:
Originally Posted by rituparna_gupta
Code:
sed "s/${b}/15/g" abc.txt

Have tried various forms of sed (with single quotes, without quotes, with -e option, etc) but getting the same error with all:
Code:
sed: -e expression #1, char 0: no previous regular expression

You are getting that error because the shell variable, ${b}, is unset or null. After its expansion, the sed command becomes s//15/g. In sed, an empty regular expression is equivalent to the most recently used regular expression. Since there is none, you get that error.

Regards,
Alister
# 3  
Old 07-08-2012
Thanks Alister...

Is there any way this can be overcome - so that I can have variable in the file referenced directly
# 4  
Old 07-08-2012
how about:
Code:
sed "s/^b=.*/b=${b}/" abc.txt

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 07-09-2012
It worked - Thanks Chubler_XL Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed to replace variable in file

Hi All I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below 123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting 348384y, this is my name <<fileabc.txt>>, I stay at near the mall of... (8 Replies)
Discussion started by: reldb
8 Replies

2. Shell Programming and Scripting

Sed/replace using variable

Hello everyone, In one of my shell script I am doing sed/replace using a variable to find a string in a file & replace it with another string in same file. The challenge which I am facing is that, the variable which I am using with "sed" is having one of character as "/" Here is below how... (4 Replies)
Discussion started by: gr8_usk
4 Replies

3. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

4. Shell Programming and Scripting

sed/awk script to replace only FIRST comment in the file

My first comment on every file contains the license message. I want to replace with a new license message. I used the below sed script, which replaces all comments. What is the modification or any other method with awk script for the below to edit only the first comment(license message)? #sed -f... (1 Reply)
Discussion started by: vpshastry
1 Replies

5. 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

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

Replace a var in sql file with shell script variable

I have a requirement where i have a sql file (filetext.sql). This file contains a variable ss_code. Now in a shell script im trying to replace the variable ss_code with a value contained in the shell script variable MTK_DC..tried the below in the script MTK_DC="mit,cit,bit" OUT=`awk -v... (4 Replies)
Discussion started by: michaelrozar17
4 Replies

8. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

replace with value of variable using SED

Hello everyone, I need to replace a pattern with "value of a variable" using sed but no luck. i tried the below ABC=STR2 sed 's/STR1/$ABC/g' <file> (also tried \ , $ and " in combination but failed) Appreciate any help. Thanks Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question