- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Find and replace text string script
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
07-01-2003 08:25 AM
07-01-2003 08:25 AM
I could really use some help with a task I have been working on. I'm in the process of moving an application from one server to another. Part of this process involves me searching a directory and all sub-directories below it for text files containing the old server name and replacing that with a fully qualified DNS name.
I have been searching and reading MAN pages for a day now but have not been able to gain any ground. I have come up with a command to find all the files that need to be changed but I???m at a loss for the command to replace the text string.
# find . ???depth ???exec grep ???l ???serverA??? {} \;
Any help would greatly be appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:28 AM
07-01-2003 08:28 AM
Re: Find and replace text string script
find . -name oldhostname -print -exec mv oldhostname newhostname.your.domain {} \;
To just find them:
find . -name oldhostname -print
Hope it helps
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:34 AM
07-01-2003 08:34 AM
Re: Find and replace text string script
You could do a
find . -name oldhostname -print > jjp.out
Then edit the file to add the mv command wrapped around the file names...
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:40 AM
07-01-2003 08:40 AM
Re: Find and replace text string script
do
echo Saving old file $FILENAME
cp -p $FILENAME $FILENAME.orig
echo Changing old to new
cat $FILENAME.orig | sed s.OLDHOST.NEWHOST.g > $FILENAME
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:43 AM
07-01-2003 08:43 AM
Re: Find and replace text string script
If it is something within the file, you can use your find statement and send the resulting files that will need to be modified to a file.
# find n. -depth -exec grep -i 'serverA' {} \; > /tmp/filename
Then you can work from /tmp/filename and use sed to search thru each file and do your substitution.
Something like:
# for i in $(cat /tmp/filename)
do
sed with some options against $i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:45 AM
07-01-2003 08:45 AM
Re: Find and replace text string script
You could do something like:
#!/usr/bin/sh
FILE=/tmp/results
cd /tmp/dummydir
find . -print |xargs grep hi > ${FILE}
OLDIFS=${IFS}
IFS=:
while read INPF X
do
sed -e 's/hi/bye/g' ${INPF} > ${INPF}.new
[ -f "${INPF}.new" ] && mv ${INPF}.new ${INPF}
done < ${FILE}
exit 0
In this example, I have searched for "hi" and replaced "hi" by "bye".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:46 AM
07-01-2003 08:46 AM
Solutionsed -e 's/serverA/serverB /g'
Where serverA is the old server name and
serverB is the new server name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:48 AM
07-01-2003 08:48 AM
Re: Find and replace text string script
for i in `find . -depth -exec grep -l 'serverA' {} \; | sed 's/\.\///'`
do
sed 's/serverA/serverA.ur.domain/' $i > $i.m
mv $i.m $i
done
I'd make copies of the files first, you just can't be too sure.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:53 AM
07-01-2003 08:53 AM
Re: Find and replace text string script
I have made copied a directory to my local machine (not working with production data)
I have to search each text file, and only text files, located in this directory and every sub-directory within this directory for a string of text "serverA". When the string of text "serverA" is located I need to replace that string of text "serverA" with the new sting of text "serverB".
There are literally 100's of text files located in this directory... So I'm trying to put a script together to accomplish this task... another thing I need to keep in mind is that disk space is limited in production.
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2003 08:53 AM
07-01-2003 08:53 AM
Re: Find and replace text string script
For instance-
perl -p -i -e 's/ServerA/ServerA.xyz.com/g' `find . ???depth ???exec grep ???l ???serverA??? {} \;`
Putting back-quotes (`) around the find will be the results onto the command line. perl then will apply the change through the entire list of filenames and save the changed file back into the original filename.
Note- A backup of the folder would be a prudent idea...
Note- If you have too many file names it will exceede the shells maximum command line length. In that case you could use-
find . ???depth ???exec grep ???l ???serverA??? {} \; | xargs perl -p -i -e 's/ServerA/ServerA.xyz.com/g'
Do a man on xargs, it is handy for breaking up command lines into managable blocks.
HTH
-- Rod Hills