- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Finding and replacing word pattern in all files an...
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
11-04-2008 12:14 PM
11-04-2008 12:14 PM
I want to find a particular word pattern and replace it with other pattern in all files in current directory and files in the subdirectories of the current directory.
Can someone let me know what command and options should be used ?
Thanks,
Shiv
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 12:21 PM
11-04-2008 12:21 PM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
find . -type f -exec sed 's/foo/bar/g' {} \;
(I think - I didn't have a chance to check it).
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 12:39 PM
11-04-2008 12:39 PM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
find . -type f -exec sed 's/abc/pqr/g' {} \;
or
find . -type f|xargs sed 's/abc/pqr/g'
would chnge the word you want to change and prrint it on your terminal.
It would not save the files.
Do you want to save the files??
BR,
Kapil+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 12:47 PM
11-04-2008 12:47 PM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
Thanks,
Shiv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 01:01 PM
11-04-2008 01:01 PM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
sed.scr:
#!/bin/sh
tmpname=/tmp/sed.$$
sed 's/
cp $tmpname $1
rm $tmpname
then:
find . -type f -exec sed.scr {} \;
ought to be a close (I didn't check or test).
note that sed in hpux doesn't have the ability to do the changes "in-place", which is what requires the copy stuff. GNU variants have that operation, so that you don't need to redirect the output and copy it back.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 01:04 PM
11-04-2008 01:04 PM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
You can use 'sed' to perform the substitution, but then you would have to redirect the output to a file of a *different* name. Then you have to rename the new output file to be that of the old input file to replace it.
This can be accomodated with:
# cat /usr/local/bin/mything
#!/usr/bin/sh
sed -e s'|string1|string2|g' ${1} > ${1}.new
mv ${1}.new ${1}
...and do:
# find . -type f -exec /usr/local/bin/mything {} \;
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 05:33 PM
11-04-2008 05:33 PM
SolutionBill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2008 08:26 PM
11-04-2008 08:26 PM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
You can use perl like you did, or you could use ex(1):
for i in $*; do
ex $i <
wq
EOF
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2008 08:11 AM
11-05-2008 08:11 AM
Re: Finding and replacing word pattern in all files and subdirectories underneath it
As BIll noted, "There is no direct way to insert a change inside a file".
This is true even in the slight-of-hand that Perl plays when it does "in-place" updates as :
# perl -pi.old -e 's/old/new/' file
Under the covers, a temporary file is created and a 'rename()' is performed to save the original copy. The beauty is in that Perl makes hard things easy.
Regards!
...JRF...