- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Search n replace recursively
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
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
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
02-19-2013 04:32 AM - last edited on 02-19-2013 06:13 PM by Cathy_xu
02-19-2013 04:32 AM - last edited on 02-19-2013 06:13 PM by Cathy_xu
Search n replace recursively
Hi ,
/test with /home/test123
need to replace only /test by /work .
Only "/test" , it must not leave test123 as it is .
How do i search and replace recursively ?? for many files
P.S.This thread has been moved from HP-UX >System Administration to HP-UX > languages-HP Forums Moderator
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2013 05:35 AM
02-19-2013 05:35 AM
Re: Search and replace recursively
You could use "find ... -exec magic-script {} +, where magic-script is a sed or perl script that will change the string.
sed -e 's:/test:/work:' file > file.new
>Only "/test", it must not leave test123 as it is.
Did you really mean "not" above? My code will replace only the first "/test" by "/work" but also "/teststuff".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2013 07:00 PM
02-19-2013 07:00 PM
Re: Search and replace recursively
Oops my mistake.
I meant it must leave test123 as it is.
It must replace /test with /work only in all recursive sub-directory .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2013 10:58 PM
02-19-2013 10:58 PM
Re: Search and replace recursively
>Oops my mistake.
(You can use Post Options > Edit Reply to correct it.)
If you only want to change /test, is it always followed by a space? What are all possible delimiters?
Or is it anything that's NOT [A-Z_a-z0-9]?
sed -e 's:/test\([^A-Z_a-z0-9]\):/work\1:' file > file.new
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2013 11:34 PM
02-19-2013 11:34 PM
Re: Search and replace recursively
Hi ,
Its only /test always followd by nothing.
its a mountpint replacemnt taken from different server.
/test to replaced with /work only ....many files.
/test123 not to be touched
want to do it using perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2013 12:07 AM
02-20-2013 12:07 AM
Re: Search and replace recursively
>Its only /test always followed by nothing.
sed -e 's:/test$:/work:' file > file.new
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2013 09:31 PM
02-20-2013 09:31 PM
Re: Search and replace recursively
Hi Dennis ,
How can i achieve this for all subdirectory .
In short a one liner sed or perl would be preferable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2013 09:33 PM
02-20-2013 09:33 PM
Re: Search and replace recursively
Also for me this ones working fine ;
sed -e 's:/test\([^A-Z_a-z0-9]\):/work\1:' file > file.new
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2013 10:27 PM
02-20-2013 10:27 PM
Re: Search and replace recursively
>How can I achieve this for all subdirectory?
You would take the above find command and create magic-script:
#!/usr/bin/ksh
# Replaces /test by /work in all files on the command line
for file in $*; do
sed -e 's:/test\([^A-Z_a-z0-9]\):/work\1:' $file > $file.new
if [ $? -eq 0 ]; then
mv $file.new $file
fi
done
You can probably do it easier in one step in perl.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2013 10:30 PM
02-20-2013 10:30 PM
Re: Search and replace recursively
Hi ,
Can u help me with perl.