Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed command replace syntax. 1

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
Hi members,
I am trying to replace all strings that begin with &&user to user in the directory. It should replace in all files and sub directories of the directory.

I've tried this but it doesn't work. I am using solaris 5.10 ksh and sed -i also won't work.

find ./ -type f -exec sed -i 's/&&user/user/g' {} \;

What is the escape character for double ampersands? Appreciate your help. Thanks.
 
Unfortunately the -i option is a GNU extension which isn't available on the standard Solaris version of sed.

Try something like this:

Code:
# save the date for later
DATE=`date +%Y%m%d`
# for each file containing '&&user'
find . -type f | xargs grep -l '&&user' | while read FILE
do
    # back it up
    cp -p $FILE $FILE.$DATE
    # use the backup as input to sed and overwrite the original
    sed 's/&&user/user/g' $FILE.$DATE > $FILE
done

You shouldn't need to escape the ampersands; they only require escaping in the replacement string which is where they have special meaning; in that case you would just use \&.

Annihilannic.
 
(sorry Stefan, we cross-posted... yes, I was that slow to submit!)

Annihilannic.
 
I tested the script.. Its is just deleting the line with &&user. Need an alternative idea. Thanks all!
 
That's strange, worked fine for me on Solaris. Did you figure it out in the end?.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top