1847584 Members
2996 Online
110265 Solutions
New Discussion

find and replace

 
Raji Murthy
Occasional Advisor

find and replace

I have a big file which has hardcoded text I want to find and replace.

replace "/home/emorales" with the string
"/usr/local/bin"

I am using sed command it is complaining about the "/".
sed -e 's/"/home/emorales"/"/usr/local/bin"/g' filename

Does anyone out there give me some help?

By the way this unix 11.0

Thanks

be good and do good
7 REPLIES 7
Steve Steel
Honored Contributor

Re: find and replace

Hi

Put the / it complains about as \/

sed -e 's/\/usr/\/lala/' file

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
harry d brown jr
Honored Contributor

Re: find and replace

sed -e 's_/home/emorales_/usr/local/bin_g' filename


USE underscores (_)

You know you have to redirect the output to another filename, DO NOT USE THE ORIGINAL filename because it will clobber the process.

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: find and replace

Or when you want to change that in the file itself, use perl

# perl -pi -e 's:/"/home/emorales":"/usr/local/bin":g' filename
Enjoy, Have FUN! H.Merijn
Louis A. Lopez
Occasional Advisor

Re: find and replace

Hi,
I tried it using the sed command.
Here is what I got:

$ echo "/home/emorales" | sed 's/home\/emorales/usr\/local\/bin/'
/usr/local/bin

Hope it helps.
-Louis
Kenny Chau
Trusted Contributor

Re: find and replace

You can put a "\" in front of the "/", eg.

sed -e 's/\/home\/user/\/home\/userabc/g'
- - - -

This will tell to treat the next character as a normal symbol.

Hope this helps.
Kenny.
Kenny
Trond Haugen
Honored Contributor

Re: find and replace

For a big file sed is probably the best but as I love vi I can't ressist a solution for that.
:g/\/home\/emorales/s//\/usr\/local\/bin/g

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Bill McNAMARA_1
Honored Contributor

Re: find and replace

#!/usr/bin/sh
#
# replace text in file
#
# by bill mcnamara wimac@tidhom1g.grenoble.hp.com wma@teamlog.com
#set -x

if [ $# -lt 3 ];
then
echo "$0 old new /file.xml"
exit 1
fi

typeset OLD=$1
typeset NEW=$2
typeset FILE=$3

# cleanup

# kludge for sed end of file 'bug'
echo "" >> $FILE

# don't want to look at binaries
sed "s:$OLD:$NEW:g" $FILE > ${FILE}.bak
cp $FILE.bak $FILE

# summarise
#.end.
It works for me (tm)