- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace Multiple Strings on 1 file
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
Discussions
Discussions
Forums
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
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
тАО04-26-2004 12:50 AM
тАО04-26-2004 12:50 AM
I am trying to perform the following now I can't use perl I need to replace multiple patterns in a file e.g
for p in 1 2 3 4
do
oldfile=`grep $p /tmp/old`
newfile=`grep $p /tmp/new`
sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
done
now I need the changes for all instances of the for loop to be saved in the 1 file any suggestions ????
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 12:54 AM
тАО04-26-2004 12:54 AM
Re: Replace Multiple Strings on 1 file
sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
with
sed "s%${oldfile}%${newfile}%g" FILE1 >> FILE2
Regds,
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 01:04 AM
тАО04-26-2004 01:04 AM
Re: Replace Multiple Strings on 1 file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 01:04 AM
тАО04-26-2004 01:04 AM
Re: Replace Multiple Strings on 1 file
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 01:33 AM
тАО04-26-2004 01:33 AM
Re: Replace Multiple Strings on 1 file
I have a couple of servers in a list in a file so the for loop edits the file for each server.
Now FILE1 is the file in which the patterns are found
Script
newdate=`date`
for p in `cat servername`
do
oldfile=`grep $p FILE1`
newfile= $p$newdate
sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
done
cp FILE1 FILE.bck
mv FILE2 FILE1
FILE 1 before the for loop
CONTENT
servera.olddate
serverb.olddate
serverc.olddate
serverd.olddate
servere.olddate
Content
What I want to see
FILE 2
Content
servera.newdate
serverb.newdate
serverc.newdate
serverd.newdate
servere.olddate
Content
I want the output to look like
Now FILE1 has other content and I only need the multiple patterns changed but if I use a double >> it will direct all the contents over and over for each instance of the for loop
aaaabbbb
ccccdddd
eeeeffff
Now say I need to change patterns aaaa and cccc to new paterns xxxx and yyyy so I perform the following
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 02:50 AM
тАО04-26-2004 02:50 AM
Solutionnewdate=`date +'%Y%m%d%H%M%S'`
cp FILE1 FILE.bck
for p in `cat servername`
do
oldfile=`grep $p FILE1`
newfile="${p}.${newdate}"
sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
mv FILE2 FILE1
done
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 02:58 AM
тАО04-26-2004 02:58 AM
Re: Replace Multiple Strings on 1 file
newdate=`date +'%Y%m%d%H%M%S'`
cp FILE1 FILE.bck
cat /dev/null >SED1
for p in `cat servername`
do
oldfile=`grep $p FILE1`
newfile="${p}.${newdate}"
echo "s%${oldfile}%${newfile}%g" >>SED1
done
sed -f SED1 FILE1 > FILE2
mv FILE2 FILE1
live free or die
harry