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 )
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 ?
Try this...
ToSearch="\*\*\* to search \*\*\*\*"
root@bt:/tmp# cat inputfile
**** ahamed ****
ahamed ahamed
root@bt:/tmp# srch='\*\*\*\* ahamed \*\*\*\*'
root@bt:/tmp# sed "s/$srch//g" inputfile
ahamed ahamed
--ahamed
thanks ahamed
i want to use the script for general purpose like
srch
can contain any text , like i don't want to hardcode the script for only
"*** to search ***"
pattern
How can we achieve that ?
Not sure if this can be done in a better way...
#!/bin/bash
srch='**** ahamed ****'
srch=$(echo "$srch" | sed 's/\*/\\\*/g')
sed "s/$(echo "$srch")//g" input_file > output_file
--ahamed