problem with sed command in shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with sed command in shell script.
# 1  
Old 09-20-2006
problem with sed command in shell script.

Guys,
I've a problem in the "sed" command used in my shellscripts

This is the problamatic line in my shell script:
sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt
It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'.

However If I replace the line as below(by replacing the actual string)
sed -e 's/A,P,CMD,20-SEP-06,2006/A,P,CMD,20-SEP-06,20062814/g' prod_hier_1234.txt>test.txt
it works..

Could someone provide thougs about what could be the problem and the possible solution?

Thanks,
Bhagat
# 2  
Old 09-20-2006
Quote:
Originally Posted by bhagat.singh-j
Guys,
I've a problem in the "sed" command used in my shellscripts

This is the problamatic line in my shell script:
sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt
It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'.

However If I replace the line as below(by replacing the actual string)
sed -e 's/A,P,CMD,20-SEP-06,2006/A,P,CMD,20-SEP-06,20062814/g' prod_hier_1234.txt>test.txt
it works..

Could someone provide thougs about what could be the problem and the possible solution?

Thanks,
Bhagat
Hi,

I haven't tested it but don'y you have to double quote your sed scripting instead of single when using variables?
sed -e "s/${line1}/${line1_m}/g" prod_hier_1234.txt > test.txt
# 3  
Old 09-21-2006
With
sed -e "s/${line1}/${line1_m}/g" prod_hier_1234.txt > test.txt
.....
It throws the error message "sed: Function s/ cannot be parsed."..
Oooops..
# 4  
Old 09-21-2006
weird solution

try the follwing
Code:
line1="abc"
line2="efg"
echo "sed -e 's/$line1/$line2/g' prod_hier_1234.txt > test.txt" > temp.sh
sh temp.sh
rm temp.sh

hope this works Smilie
# 5  
Old 09-21-2006
try this

Code:
sed -e 's/'${line1}'/'${line1_m}'/g' prod_hier_1234.txt > test.txt

# 6  
Old 09-21-2006
error given:-

Code:
$ sed -e 's/'${line1}'/'${line1_m}'/g' prod_hier_1234.txt > test.txt
sed: -e expression #1, char 0: no previous regular expression

# 7  
Old 09-21-2006
may be

May be you have a expression which consists of many words. Try enclosing the entire expression in quotes and see. But I am not sure of this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command not accepting variable in shell script

I am using a shell script in fedora linux. While calling to the shell I am also passing an argument (var1=0.77) like shown below sh gossip.sh var1=0.77 in the shell following command is written (which doesn't work) sed - i -e 's@prob=@prob="$var1";//@g' file.txt Actually i want the... (7 Replies)
Discussion started by: Fakhar Hassan
7 Replies

2. Shell Programming and Scripting

Problem with expr command in shell script

Hi, I have used expr command to increment the date. for e.g., case 1 : echo $(date -d $(echo `expr 20010101 + 1`)) it returns Tue Jan 2 00:00:00 IST 2001 case 2: echo $(date -d $(echo `expr 20010101 - 1`)) it returns date: invalid date `20010100' please suggest me, how to... (3 Replies)
Discussion started by: nanthagopal
3 Replies

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

4. Shell Programming and Scripting

Shell Script problem with sed

Hello i have a shell script which will go in some folders using for loop, build some programs with gcc and zip the output. then i have to paste the file of that zip file into a yml file which will be further used by a ruby watir script. PROJECT_PATH=/uploads/$PROJECT_FOLDER zip -rq ... (0 Replies)
Discussion started by: vikki1986
0 Replies

5. Shell Programming and Scripting

Using SED command in a shell script: Unterminated address regex

Hi All, I am trying to use a sed command in a shell script in order to delete some lines in a file and I got the following error message. I don't understand why it is not working 'cause I have tried with simple quotes, then with double-quotes, and it is not working. sed: -e expression #1,... (7 Replies)
Discussion started by: Alpha3363
7 Replies

6. Shell Programming and Scripting

shell script/sed command help

First off I have read the man pages for sed and am still having trouble working on a script to remove portions of a log: My goal is to take a log file to be emailed, read the file and strip the portions away AFTER the line MIME-Version:1.0 and strip away until it to the line starting with... (4 Replies)
Discussion started by: murphybr
4 Replies

7. Shell Programming and Scripting

Problem with MV command in Shell Script

Hi Guru's, I 'm trying to execute the below given script in Unix. I am having an issue with the script. The output of the script is given below: #!/bin/bash File_Home="/home/essftp/Risk" cd /home/essftp/Risk rm -f FileList rm -f credit_risk* file1=`ls -lt... (4 Replies)
Discussion started by: ranjith_taurean
4 Replies

8. Shell Programming and Scripting

Need help using sed command in shell script?

Hello, i want to take the input from user and according to that variable's value search in file emp.lst. Here is what i came up with echo -e "Enter string to be searched :\c" read str sed -n '/\$str/p' emp.lst this is not working! any idea why?Thanks in advance! :) (4 Replies)
Discussion started by: salman4u
4 Replies

9. Shell Programming and Scripting

Quick Question on sed command in shell script

Hello, I have the following line in one of my shell scripts. It works fine when the search string($SERACH_STR) exists in the logfile($ALERTLOG) but if the search string does not exist this line errors out at run time. Is there a way to make this line return 0 if it is not able to find the... (4 Replies)
Discussion started by: luft
4 Replies

10. Shell Programming and Scripting

Sed command in shell script

I have a current code working(named subst1) having a user be able to type this line to substitute words using the sed command: subst1 old-pattern new-pattern filename Here is my shell script: #!/bin/bash # subst1 ARGS=3 E_BADARGS=65 if then echo "Usage: `basename $0`... (1 Reply)
Discussion started by: Todd88
1 Replies
Login or Register to Ask a Question