- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Replace 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
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
03-29-2006 04:51 PM
03-29-2006 04:51 PM
Replace string
eg .
the original file is as below ,
aaa bbb ccc ddd
111 222 333 444
### %%% &&& ***
aaa bbb ccc ddd
111 222 333 444
### %%% &&& ***
if I want to replace all string "aaa bbb ccc ddd" with "sss ttt uuu vvv" , the result become to below , can suggest how to do it ? thx
sss ttt uuu vvv
111 222 333 444
### %%% &&& ***
sss ttt uuu vvv
111 222 333 444
### %%% &&& ***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 04:57 PM
03-29-2006 04:57 PM
Re: Replace string
sed 's/aaa bbb ccc ddd/sss ttt uuu vvv/' source_file > dest_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 05:06 PM
03-29-2006 05:06 PM
Re: Replace string
but if I want to direct update the file , the above method seems generate a new file , how to do it ? thx.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 05:13 PM
03-29-2006 05:13 PM
Re: Replace string
and then press shift : so you are in command mode and then type
s/aaa bbb ccc ddd/sss ttt uuu vvv/g
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 05:24 PM
03-29-2006 05:24 PM
Re: Replace string
cp original_file /tmp/original_file.tmp
sed -e "s/aaa bbb ccc ddd/sss ttt uuu vvv/" /tmp/original_file.tmp > original_file
rm /tmp/original_file.tmp
see man sed for more.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 08:28 PM
03-29-2006 08:28 PM
Re: Replace string
perl -pe "s/aaa bbb ccc ddd/sss ttt uuu vvv/g" -i your_file your_file
This will chnage teh string in Your_file without showing teh result on stdlist.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 08:53 PM
03-29-2006 08:53 PM
Re: Replace string
:1,$s/aaa bbb ccc ddd/sss ttt uuu vvv
that's all
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 09:55 PM
03-29-2006 09:55 PM
Re: Replace string
use ex:
print 'g/aaa bbb ccc ddd/s//sss ttt uuu vvv/g\nw' | ex /tmp/file
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2006 11:38 PM
03-29-2006 11:38 PM
Re: Replace string
>> but if I want to direct update the file , the above method seems generate a new file , how to do it ? thx.
Please note that all of the methods outlined really create a brand new file which happens to look much the same as the original.
If you truly want to update in place, then you need a tool like a C program or PERL script which opens the file in update mode: OPEN file, "+
Google for : +perl +"open for update"
But I don't think that's what you actually want.
Also... what is the real problem you are trying to solve?
your data pattern _suggests_ that this is not a random replace, but perhaps driven by information from an other file/source. If you share that with us, then maybe we can come up with a better solution/script for the larger problem.
Best regards,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2006 01:39 AM
03-30-2006 01:39 AM
Re: Replace string
I think my 'ex'-solution will - permissions granted - non-interactively produce a file with the same name as the original one but containing the requested changes (Art's solutions as well).
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2006 03:32 AM
03-30-2006 03:32 AM
Re: Replace string
Well Peter, I know it is nitpicking, but I beg to differ
'ex' is just a nice userfriendlier jacket around the standard editor which takes care of the hidding the temp file. But I could be wrong
From the man page:
"Current file. The name of the file being edited by ex is called the current file. Text from the current file is read into a work area, and all editing changes are performed on this work area. Changes do not affect the original file until the work area is explicitly written back to the file. If the % character is used as a file name, it is replaced by the current file name."
So the whole buffer is wrtten into a new file, which happens to have the same name.
Ditto for perl. The -i argument tells Perl to make the change in-line using the file for both reading and writing. If a -i is followed by a file extensiona backup of the file is created before the program is run. But really that's taking place anyway.
Try it on a good sized (50+ MB?) file.
A true inplace replace would read the whole file and just write those blocks that have changed data. So for 50MB, then would be 6000 or so read IOs and say 10 write IOs. I don't think that's what 'ex' nor 'perl -i' will do.
A true in-place replace can also only be done if the data size does not change. (which would be the case in the example provided)
Again, I'm just nitpicking.
The topic author is 99.99% sure to be just looking for the 'ex' or 'perl -i' solution and is unlikely to mean a literal 'in place' where just the changed blocks get written to.
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2006 08:11 PM
03-30-2006 08:11 PM
Re: Replace string
of course you are correct, and that's why I told '... produce a file with the same name as the original one ...'.
I should have added
... but the inode will be different.
I think there are others in this forum, who have written a 'change_string_in_file.c' to patch (binary) files and missed to check the lengths of the search and replace strings in their first attempt correctly...
BTW, ivychung2, did you proceed in your efforts in string replacement?
mfG Peter