- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Trying this agian... Find and replace text string ...
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
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-01-2003 09:33 AM
тАО07-01-2003 09:33 AM
I have copied a directory to my local machine (not testing with production data)
I need to search for a string of text, that being 'serverA' and change it to 'serverB'. Now I know for a fact that this sting of text will be located in 100's of text files and even be part of directory names. This string of text 'serverA' is located in literally hundreds of places with in this directory and all of the sub-directories under it. When the string of text "serverA" is located I need to replace that string of text "serverA" with the new sting of text "serverB". So in short I need to replace 'serverA' which is located both inside files and directory names.
I thought someone provided the answer on my previous post but after further investigation none of the text strings were replaced.
Thank you again for all of your help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 09:36 AM
тАО07-01-2003 09:36 AM
Re: Trying this agian... Find and replace text string script.
Its a perl script.
It scans for text, creates a filelist and makes the change.
Hope it helps.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 09:37 AM
тАО07-01-2003 09:37 AM
Re: Trying this agian... Find and replace text string script.
I think you did get good answers in the previous thread, but I would suggest you break this down into two passes: one for files using "find -type f" and one for directories using "find -type d" as the processing requirements will be different.
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 09:40 AM
тАО07-01-2003 09:40 AM
Re: Trying this agian... Find and replace text string script.
The following will work for making changes within a file that you have open with vi.
:g/serverA/s//serverB/g
This will make a global change of serverA to serverB, inside of a file...using vi.
-Bryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:09 AM
тАО07-01-2003 10:09 AM
Re: Trying this agian... Find and replace text string script.
replacing the text string in those files. The
below command should work:
grep -l serverA * | sed "s/serverA/serverB/g
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:17 AM
тАО07-01-2003 10:17 AM
Re: Trying this agian... Find and replace text string script.
#!/usr/bin/ksh
for file in $(ls -R)
do
if [-f $file]
then
vi $file << EOF
:1,$ s/serverA/serverB/g
:wq!
EOF
elif [-d $file]
then
newfile=$(echo $file ex "s/serverA/serverB/g")
mv $file $newfile
fi
done
This will EDIT the files & move the directories. I have not tested it, so you may need to tweek.
Regards
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:23 AM
тАО07-01-2003 10:23 AM
Re: Trying this agian... Find and replace text string script.
You haven't mentioned why none of the solutions are working. Some feedback could help the community give you a solution.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:25 AM
тАО07-01-2003 10:25 AM
Re: Trying this agian... Find and replace text string script.
sed "s/serverA/serverB/g" `find . -type f -print | xargs grep -l serverA`
That will find all the files from the directory you start the command from with the text string serverA and replace it with serverB.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:32 AM
тАО07-01-2003 10:32 AM
Re: Trying this agian... Find and replace text string script.
sh [3]: [-f: not found.
sh [9]: [-d: not found.
#!/usr/bin/ksh
for file in $(ls -R)
do
if [-f $file]
then
vi $file << EOF
:1,$ s/serverA/serverB/g
:wq!
EOF
elif [-d $file]
then
newfile=$(echo $file ex "s/serverA/serverB/g")
mv $file $newfile
fi
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:45 AM
тАО07-01-2003 10:45 AM
Re: Trying this agian... Find and replace text string script.
Your script is running but it is not changing any of the files or directory names.
Just trying to provide feed-back.
Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 10:59 AM
тАО07-01-2003 10:59 AM
Re: Trying this agian... Find and replace text string script.
for i in `find . -depth -exec grep -l 'serverA' {} \; | sed 's/\.\///'`
do
sed 's/serverA/serverB/' $i > $i.m
mv $i.m $i
done
The first sed gets rid of the "./" from the find, assuming you're in the top of the directory you want to do the mods in.
As for changing the directory names, it pretty much the same.
for d in `find . -name *serverA* -type d -exec ls -d {} \; | sed 's/\.\///'`
do
mv $d $d.com
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 11:03 AM
тАО07-01-2003 11:03 AM
Re: Trying this agian... Find and replace text string script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 11:10 AM
тАО07-01-2003 11:10 AM
Re: Trying this agian... Find and replace text string script.
You can use "sed" as long as you iterate over each filename, have sed input each file one at a time, output the result to one file, and then replace the original file with the newly created output file.
A more efficient "find" statement would be-
find . -print | xargs grep -l
You original "find" calls grep for each filename. By using xargs you can call "grep" fewer times and reduce overhead and time.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 11:10 AM
тАО07-01-2003 11:10 AM
Re: Trying this agian... Find and replace text string script.
You can use "sed" as long as you iterate over each filename, have sed input each file one at a time, output the result to one file, and then replace the original file with the newly created output file.
A more efficient "find" statement would be-
find . -print | xargs grep -l "ServberA"
You original "find" calls grep for each filename. By using xargs you can call "grep" fewer times and reduce overhead and time.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 11:19 AM
тАО07-01-2003 11:19 AM
Re: Trying this agian... Find and replace text string script.
Thank you for the update... but I'm lost... can you dumb it down for me a bit and give me more detail?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 11:27 AM
тАО07-01-2003 11:27 AM
SolutionThis is one way:
#!/usr/bin/sh
FILE=/tmp/results
cd /tmp/dummydir #...change this...
find . -type f -print|xargs grep -l serverA > ${FILE}
while read INPF X
do
sed -e 's/serverA/serverB/g' ${INPF} > ${INPF}.new
[ -f "${INPF}.new" ] && mv ${INPF}.new ${INPF}
done < ${FILE}
rm ${FILE}
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 11:43 AM
тАО07-01-2003 11:43 AM
Re: Trying this agian... Find and replace text string script.
for i in `find . -name *serverA* -type d -exec ls -d {} \;`
do
echo $i
newdir=`echo $i |sed 's/serverA/serverB/' `
mv $i $newdir
done
make a backup before trying anyhting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 12:37 PM
тАО07-01-2003 12:37 PM
Re: Trying this agian... Find and replace text string script.
the "if [ -f $file ]" means if $file is a file, and "elif [ -d $file ]" mean else if $file is a directory. They SHOULD work if not try man ksh & look for the relavent section. Currently I'm @home & do not have a computer to try this out on (sorry :-( )
Alternatively there are alot of replies using find (an fine solution, probably more efficient too)
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2003 12:50 PM
тАО07-01-2003 12:50 PM
Re: Trying this agian... Find and replace text string script.
Unix shell programming can be frustrating at first, but with experience it can do some fantastic things (and perl even more so)!
-- Rod Hills