Sed emptying file when i use for search replace operation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed emptying file when i use for search replace operation
# 1  
Old 10-28-2011
Sed emptying file when i use for search replace operation

Hi Friends

I am new to sed programming , i found that the below code can search for the $ToSearch and Replace it with $ToReplace ( $ToSearch and $ToReplace are my variables in my script )

Code:
sed "s/$ToSearch/$ToReplace/" $file > $output
mv $output $file

In testing the script i found that this is emptying the file if
$ToSearch="*** to search ****"
Note: i want to search for the string "*** to search *** " [ '*' not to replace with any character ]
How to handle with the wild characters in search and replace operations ?
# 2  
Old 10-28-2011
Try this...
Code:
ToSearch="\*\*\* to search \*\*\*\*"

Code:
root@bt:/tmp# cat inputfile 
**** ahamed ****
ahamed ahamed
root@bt:/tmp# srch='\*\*\*\* ahamed \*\*\*\*'
root@bt:/tmp# sed "s/$srch//g" inputfile 

ahamed ahamed

--ahamed
# 3  
Old 10-28-2011
thanks ahamed

i want to use the script for general purpose like
Code:
srch

can contain any text , like i don't want to hardcode the script for only
Code:
"*** to search ***"

pattern

How can we achieve that ?
# 4  
Old 10-28-2011
Not sure if this can be done in a better way...
Code:
#!/bin/bash
srch='**** ahamed ****'
srch=$(echo "$srch" | sed 's/\*/\\\*/g')
sed "s/$(echo "$srch")//g" input_file > output_file

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

3. Shell Programming and Scripting

sed to search and replace - iterating on a file

Hi, I am new to sed scriping but do have some basic scripting skills. I have a properties file props.conf with name/value pairs written in it. Sample is Name1=test Name2=notest Then I have a template file (can be xml or simple text) which will have 'keys' embedded in it. Keys can... (2 Replies)
Discussion started by: tigerinopen
2 Replies

4. Shell Programming and Scripting

sed command to skip the first line during find and replace operation

Hi Gurus, I did an exhaustive search for finding the script using "sed" to exclude the first line of file during find and replace. The first line in my file is the header names. Thanks for your help.. (4 Replies)
Discussion started by: ks_reddy
4 Replies

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

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

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

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

9. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

10. 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
Login or Register to Ask a Question