Operating System - HP-UX
1834500 Members
3220 Online
110068 Solutions
New Discussion

Replace string1 with string2

 
SOLVED
Go to solution
Andrej Vavro
Frequent Advisor

Replace string1 with string2

Hi all,

I want to replace a string1 with a string2 in several acsii files using a script. What's the best and simpliest solution.

Thanks in advance.
Andrej
11 REPLIES 11
Mark van Hassel
Respected Contributor
Solution

Re: Replace string1 with string2

HI,

Try this:

for i in $(ls)
do
sed 's%string1%string2%g' $i >$i.new
mv $i $i.org
mv $i.new $i
done


HtH,

Mark
The surest sign that life exists elsewhere in the universe is that none of it has tried to contact us
Shannon Petry
Honored Contributor

Re: Replace string1 with string2

There is no way of doing this unless you make temp copies of your data. (or go interactive)

sed allows string replacement and is very good.
TMPFILE=/tmp/temp.txt
if [ -f $TMPFILE ] ; then
rm -f $TMPFILE
fi
touch $TMPFILE
cat myascii.txt | sed -e s/"string1"/"string2"/g >>$TMPFILE

Pattern matching is difficult to grok, so if your not good with pattern matching I'll recommend "SED and AWK" published by O'Reilly and Associates. The first 2 chapters are dedicated to this concept.
for more info on args to sed, see:
man sed
Regards,
Shannon
Microsoft. When do you want a virus today?
Craig Rants
Honored Contributor

Re: Replace string1 with string2

Use sed

I would use...

for i in `cat filelist`
do
sed 's/string1/string2/g' $i > $i.new
done

you can then move or copy, whatever suits you, the new file to the original

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Bill McNAMARA_1
Honored Contributor

Re: Replace string1 with string2

#!/usr/bin/sh
#
# replace text in file
#
#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)
Jeff Schussele
Honored Contributor

Re: Replace string1 with string2

Hi Andrei,

Use sed
Create a file containing the list of files you wish to work on.

Then use

for x in filelist
do
sed 's/oldstring/newstring/g $x >$x.out
done

You can include a mv command if you don't wish to keep the org file. The g forces the search/replace globally in the file - w/o it, it will only work on the first occurrence.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
harry d brown jr
Honored Contributor

Re: Replace string1 with string2

#!/usr/bin/ksh
find /tmp -type f | while read FNAME
do
sed "s/OLDSTRING/NEWSTRING/g" < ${FNAME} > ${FNAME}.tmp
mv ${FNAME}.tmp $FNAME
done


live free or die
harry
Live Free or Die
S.K. Chan
Honored Contributor

Re: Replace string1 with string2

Sed is the simplest, not neccessarily the best . Assuming the dir has only files.
# cd /dirA
# for file in `ls`
>do
>sed 's/string1/string2/g' $file > $file.replaced
>done

Also check out some handy one-liner sed ..
http://www-h.eng.cam.ac.uk/help/tpl/unix/sed.html
Bill McNAMARA_1
Honored Contributor

Re: Replace string1 with string2

Script that'll do it recursively from dir.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x2a77abe92dabd5118ff10090279cd0f9,00.html

Later,
Bill
It works for me (tm)
Jeff Schussele
Honored Contributor

Re: Replace string1 with string2

Sorry - left out the ending single quote in the sed command line. And if white space in the string you'll need quotes around it - doesn't hurt to use 'em even if no white space.
Should be as follows

sed 's/"oldstring"/"newstring"/g' $x >$x.out

Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor

Re: Replace string1 with string2

I think I will give you a really cool way to do this that takes care of all the temp file stuff behind the scenes and will even optionally create a backup.

Try this:

perl -i.save -p -e 's/oldstring/newstring/g' myfile1 myfile2 myfile3

That will edit each of the files 'on the fly' and create a backup copy of each file myfile1.save, myfile2.save. If you don't need the backup copy simply use -i rather than -i.suffix.
If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: Replace string1 with string2

Hey, Clay, that's *my* clue :)
Finaly using the command line switches hay?
Good explanation too. Something I would (deliberately) omit. Users might want to use the docs to also learn something instead of just devour what they get thrown in their hands ...

# perl -pi -e 's/old/new/' files

would have been my answer.

N/A
Enjoy, Have FUN! H.Merijn