- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how to replace strings in multiple files thru scri...
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
тАО07-27-2004 06:56 AM
тАО07-27-2004 06:56 AM
how to replace strings in multiple files thru script or command line
I have "n" files in a directory and I want to search for A/B/C/D and replace it with E/F/G/H/I in all the files thru a script or on a command line. I have to do this on 6 servers so ..
any suggestions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 07:00 AM
тАО07-27-2004 07:00 AM
Re: how to replace strings in multiple files thru script or command line
# for FILE in *
do
mv $FILE $FILE.tmp
sed 's|A/B/C/D|E/F/G/H/I|' $FILE.tmp > $FILE
[ $? -eq 0 ] && rm $FILE.tmp
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 07:20 AM
тАО07-27-2004 07:20 AM
Re: how to replace strings in multiple files thru script or command line
1) Create a file, /var/tmp/x.sed, and insert your replacement instructions.
I'll use '+' as the delimiter because that will make your "/" replacements easier.
s+A/B/C/D+E/F/G/H/I+g
You can put as many sed commands in this file as your like; they will be processed sequentially on each file.
2) Create a script to find the text files in the current directory and sed them.
#!/usr/bin/sh
TDIR=${TMPDIR:-/var/tmp}
TFILE=${TDIR}/X${$}_1.txt
SEDFILE=/var/tmp/x.sed
STAT=0
ls | while read X
do
file "${X}" | grep -q -i "text"
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
sed -f ${SEDFILE} "${X}" > ${TFILE}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
mv ${TFILE} "${X}"
fi
fi
done
exit ${STAT}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2004 08:38 AM
тАО07-27-2004 08:38 AM
Re: how to replace strings in multiple files thru script or command line
perl -p -i -e 's{A/B/C/D}{E/F/G/H/I}g' *
If you have openssh installed, you could run it on each of the servers by:
for x in server1 server2 server3 ; do
ssh $x "cd /mydir ; perl -p -i -e 's{A/B/C/D}{E/F/G/H/I}g' *"
done
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-29-2004 06:03 PM
тАО07-29-2004 06:03 PM
Re: how to replace strings in multiple files thru script or command line
for i in `cat serverlist`
do
remsh $i "sed 's/stringA/stringB/g' /dir/fileX"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-29-2004 07:01 PM
тАО07-29-2004 07:01 PM
Re: how to replace strings in multiple files thru script or command line
Limitation: If we use grep to grep a pattern on a object file ,it will make a lot of error and unreadable symbols.
In a single directory:
find . -name "*" -exec grep -q 'A/B/C/D' {} \; -print | awk '{ print "echo \"`sed -e 's%A/B/C/D%E/F/G/H%' "$1"`\"> "$1 }' | sh
Multiple directory:
Change find . to your testing directory. It will do this in their subdirectories too.
Use -type f instead of -name "*" also