- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Word substitution using SED
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
12-26-2002 02:08 AM
12-26-2002 02:08 AM
Word substitution using SED
i want to replace a word in a file,and rename the file(file with replaced word)back to the original name.I don't know the filename but i know the type of file,i.e .txt etc..and there are mulitple files of the same type..eg:- a.txt,b.txt etc..the script should open all the .txt files and replace the word if found and the files should have their original names respectively...
TIA
Praveen.K
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 02:14 AM
12-26-2002 02:14 AM
Re: Word substitution using SED
Try this :
for FILE in $(ls | grep ".txt")
do
sed -e "s/word/newword/" $FILE > $FILE.new
mv $FILE $FILE.save
mv $FILE.new $FILE
done
It should work.
Fr??d??ric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 02:19 AM
12-26-2002 02:19 AM
Re: Word substitution using SED
#!/usr/bin/sh
for file in *.txt
do
sed 's/word1/word2/g' $file >$file.$$
mv -f $file.$$ $file
done
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 04:48 AM
12-26-2002 04:48 AM
Re: Word substitution using SED
for i in *.txt
do
perl -i -e "s/word1/word2" $i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2002 09:13 PM
12-26-2002 09:13 PM
Re: Word substitution using SED
do
sed -e 's/word/replace/g' $i > $i.$$
mv $i.$$ $i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2002 12:53 AM
12-27-2002 12:53 AM
Re: Word substitution using SED
You can achieve the output you are looking at form the above responses that you have. I would like to add some points while writing a shell scripts.
1. Be sure that the temporary files that are created by the scripts are removed.
2. Be sure you have kept a backed up the original files which you intend to modify in the script
3. i do not remember more . So if i am given a request like this ... i would like to write a script like this
**************************************
#!/usr/bin/ksh
#Script to search & replace
for i in `ls *.txt` #get a list of files
do
#Replasing string "word1" in each text file with string "word2"
cat $i |sed -e "s/word1/word2/g" >$i.new
mv $i $i.old.`date` #Backup of original files
mv $i.new $i
done
***************************************
Cheers .............
kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2002 01:02 AM
12-27-2002 01:02 AM
Re: Word substitution using SED
If you need to scan a directory including it's subdirectories, you could use
find
And then create a script with the "sed" or "perl" and "mv" commands from the above examples :
file=$1
sed -e '/oldword/newword/g' $file > $file.new
mv $file.new $file
exit
You don't need the for - do - done loop anymore, because "find" will run the script for every single file found.
Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2002 08:49 AM
12-30-2002 08:49 AM
Re: Word substitution using SED
cat $ORIG_FILE | awk -v word1="$var_one" -v word2="$var_two" -v word3="$var_three" -v word4="$var_four" '
{
if ($1 == "replace_word")
{
sub(word1,word2);
sub(word3,word4);
}
} ' > $NEW_FILE
Hope this helps...
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2002 08:56 AM
12-30-2002 08:56 AM
Re: Word substitution using SED
#!/usr/bin/ksh
find /DIR -type f | while read FNAME
do
sed "s/OLDSTRING/NEWSTRING/g" < ${FNAME} > ${FNAME}.tmp
mv ${FNAME}.tmp $FNAME
done