Search and replace error using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace error using sed
# 1  
Old 05-15-2012
Search and replace error using sed

Hi,

Basically , i search for a string ( from variable) in a input file , if line found then search a string called "STRING" and replace with text (from another variable ). I cant avoid # delimiter due to esc sequences.

Code:
#!/bin/bash
LINE="source/path"
REPLACE="TEXT/TO/REPLACE"
sed -e /"$LINE"/ {
s#STRING#${REPLACE}#
} input

input contains
Code:
source/path STRING
source/path VALUE4

Desired output:
Code:
source/path TEXT/TO/REPLACE
source/path VALUE4

I get error as :
Code:
sed: can't read {: No such file or directory
/exe_1: line 5: s#STRING#TEXT/TO/REPLACE#: No such file or directory

Thanks in advance.
# 2  
Old 05-15-2012
Hi

Code:
LINE="source\/path"
REPLACE="TEXT\/TO\/REPLACE"
sed  "/$LINE/s/STRING/$REPLACE/" file

Guru.
# 3  
Old 05-15-2012
Hi,

Thanks .

As i mentioned in my post i cant avoid # delimiter. I read the values in while loop. I just gave simple example .

Any other ideas ?
# 4  
Old 05-15-2012
Quote:
Originally Posted by greet_sed
I just gave simple example .

Any other ideas ?
Give a better example.
# 5  
Old 05-15-2012
Hi,

Code:
cat FILE1
source/path
source/project/path

Code:
cat input 
"source/path" "STRING"
"source/path" VALUE4
value "source/project/path" "STRING" NEW_TEXT
min_value "source/project/dest1" Extra_here "STRING" Add_TEXT

code:
Code:
REPLACE=<output of some commands>
while read line
do
sed -e /"$line"/ {
s#STRING#${REPLACE}#
} input
done < FILE1

Hope i made it clear now.
# 6  
Old 05-15-2012
how about that?
Code:
# sed "s#$LINE STRING#$LINE $REPLACE#" input

# 7  
Old 05-15-2012
Thanks ygemici

it will not work for all the lines.

what if input contains the following ( just an example) :
Code:
"source/path" ONE TWO "STRING" THREE  "source/path" VALUE4 value "source/project/path" NEW_TEXT "STRING" min_value "source/project/path" Extra_here "STRING" Add_TEXT

Smilie

Last edited by greet_sed; 05-15-2012 at 07:57 AM.. Reason: tag added
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Search and replace within pattern

Hi Guys! Unix newbie here! Have a requirement for which I have been scouting the forums for a solution but has been out of luck so far :( I have a file which contains the following:- TEST1|TEST2|"TEST3|1@!2"|TEST5 My sed command should result in either one the following output:-... (6 Replies)
Discussion started by: hishamzz
6 Replies

2. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

3. UNIX for Dummies Questions & Answers

search and replace with sed

Hi All I have a simple text file and I want to be able to replace any alpha character and comma combination with any other symbol of my choice here is the text file I want to replace: pear apple ban,ana grape ",g1234," te,st1 here is how it should look afterwards: pear... (4 Replies)
Discussion started by: greg_b
4 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. UNIX for Dummies Questions & Answers

How to use 'sed' to search and replace?

Hello - I have a very large file in which a certain numbers are repeated. I find that using vi to edit the entire file is useless. How should i use sed to find a replace such as this text: To replace: 145.D25.D558 With: 215.22.45.DW I tried this command: sed... (4 Replies)
Discussion started by: DallasT
4 Replies

6. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

7. Shell Programming and Scripting

sed search/replace and display

Hi All: I'm using sed to replace one line in a configuration file. After replacing that line I would like to display to screen that line plus 10 lines above and below the line that was changed... How would I go about doing that?? Here is my search and replace string.... sed -e... (2 Replies)
Discussion started by: jimmyc
2 Replies

8. UNIX for Dummies Questions & Answers

Search/Replace with Sed

Is there a way to use the sed command to 1) search a specified pattern 2) in the line where that pattern is found, replace from character N to character N+4 with a new 4-character string. Thks in advance! (5 Replies)
Discussion started by: mvalonso
5 Replies

9. UNIX for Dummies Questions & Answers

sed search and replace

Hello Folks, Anyone know how I can replace this line in file.xml <oacore_nprocs oa_var="s_oacore_nprocs">8</oacore_nprocs> with this line <oacore_nprocs oa_var="s_oacore_nprocs">1</oacore_nprocs> using sed or awk ? Thanks for your time. Cheers, Dave (7 Replies)
Discussion started by: d__browne
7 Replies

10. Shell Programming and Scripting

Search and replace sed or tr

Hi folks, I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do. Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically... (6 Replies)
Discussion started by: bridgeje
6 Replies
Login or Register to Ask a Question