- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace string1 with string2
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 05:55 AM
05-23-2002 05:55 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 05:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 05:59 AM
05-23-2002 05:59 AM
Re: Replace string1 with string2
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:00 AM
05-23-2002 06:00 AM
Re: Replace string1 with string2
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:02 AM
05-23-2002 06:02 AM
Re: Replace string1 with string2
#
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:03 AM
05-23-2002 06:03 AM
Re: Replace string1 with string2
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:04 AM
05-23-2002 06:04 AM
Re: Replace string1 with string2
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:04 AM
05-23-2002 06:04 AM
Re: Replace string1 with string2
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:06 AM
05-23-2002 06:06 AM
Re: Replace string1 with string2
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x2a77abe92dabd5118ff10090279cd0f9,00.html
Later,
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:10 AM
05-23-2002 06:10 AM
Re: Replace string1 with string2
Should be as follows
sed 's/"oldstring"/"newstring"/g' $x >$x.out
Jeff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:14 AM
05-23-2002 06:14 AM
Re: Replace string1 with string2
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2002 06:52 AM
05-23-2002 06:52 AM
Re: Replace string1 with string2
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