- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: replace characters in a file
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
Forums
Discussions
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
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-05-2006 09:49 PM
07-05-2006 09:49 PM
Anybody know how to replace a charater string with a different string from a file having 1000 lines.The file is like this
aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:
I want to replace cccc with xxxxx in all lines
Thanks
Sajeesh
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 09:54 PM
07-05-2006 09:54 PM
Re: replace characters in a file
or more safe, if cccc can occur in a word like ddccccee
# perl -pi -e's/\bcccc\b/xxxxx/' file
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 09:59 PM
07-05-2006 09:59 PM
Re: replace characters in a file
You can use sed.
# sed 's/cccc/xxxxx/g' file
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 11:11 PM
07-05-2006 11:11 PM
Re: replace characters in a file
'tr' also works:
tr 'cccc' 'xxxx'
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 11:14 PM
07-05-2006 11:14 PM
Re: replace characters in a file
what you wrote here is essentially the same as
tr c x
and will change *every* occurance of 'c' to 'x' in file in every line. That's not what he wants
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 11:43 PM
07-05-2006 11:43 PM
Re: replace characters in a file
#cat file|sed 's/cccc/xxxxx/g' > newfile
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 11:48 PM
07-05-2006 11:48 PM
Re: replace characters in a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2006 11:49 PM
07-05-2006 11:49 PM
Re: replace characters in a file
# cat file | process
should be prevented wherever possible. It's just a useless extra load on the CPU, as processes are started that are not needed in a sane unix environment, where
# process < file
does exactly the same, but uses one less process. Cat can be useful if *more* input files create the input stream for process, as input redirection does not support that
# cat file1 file2 | process
Enjoy, Have (efficient) FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 12:54 AM
07-06-2006 12:54 AM
Re: replace characters in a file
lets play 'sed' a little...
I think your field delimiter is ':' - this fact was not used in the sed-solutions so far.
from=cccc
to=xxxx
sed -e "s/^$from:/$to:/" -e "s/:$from/:$to/g" input >output
But as you see from the previous responses: there is more than one way ...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 12:58 AM
07-06-2006 12:58 AM
Re: replace characters in a file
About the sharp comment of the Mr. Procura, there are not words...
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 01:16 AM
07-06-2006 01:16 AM
Re: replace characters in a file
Again, nothing personal, but yet another good oportunity to set things straight!
Enjoy, Have FUN! H.Merijn [ no points here please ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 06:56 PM
07-06-2006 06:56 PM
Re: replace characters in a file
Thanks for your answer. Sorry. I didn't make it clear.
aaa bbb ccc ddd eee fff represent strings.
and delemited with : just like passwd file.
I want to replace the 3rd field with "---"
Kindly provide the solution
Thanks
Sajeesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 07:25 PM
07-06-2006 07:25 PM
Re: replace characters in a file
#from=ccc
#to=---
#sed -e "s/$from/$to/" -e "s/$from/$to/g" 111 > 222
--Z.X.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 07:28 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 07:37 PM
07-06-2006 07:37 PM
Re: replace characters in a file
Perfect. Thanks for the promt reply
Sajeesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2006 11:14 PM
07-06-2006 11:14 PM
Re: replace characters in a file
to complete the thread, here a solution with awk:
awk -F: '$3 == "ccccc" {$3="-----"}
{print}' input >output
Or sed?
sed 's/^\([^:]*:[^:]*:\)'$from'\(.*\)/\1'$to'\2/' input >output
mfG Peter