- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Change occurrence of a string in shell
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
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
тАО09-07-2004 01:41 AM
тАО09-07-2004 01:41 AM
Change occurrence of a string in shell
quite an easy question for an experienced Unix user.
I would like to change all the occurrence of a string with another string of equal length, specifying the position of this string (related to the beginnnig of the line in the Ascii file).
For example:
123XXX789XXX
I want to specify to replace XXX with YYY in the range 4-6, this has to produce
123YYY789XXX
Any suggestion?
Thank you in advance!
Diego.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 01:49 AM
тАО09-07-2004 01:49 AM
Re: Change occurrence of a string in shell
If it is always the first on the line then this should work:
sed 's/XXX/YYY/' file_name
manish
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 01:52 AM
тАО09-07-2004 01:52 AM
Re: Change occurrence of a string in shell
I need to look for the desired string ("XXX") in a specific position, leaving the rest of the line unaltered. Could be or not be the first occurence.
Diego.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 01:53 AM
тАО09-07-2004 01:53 AM
Re: Change occurrence of a string in shell
cat file | sed -e "s/XXX/YYY/"
Olivier.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:02 AM
тАО09-07-2004 02:02 AM
Re: Change occurrence of a string in shell
you cane edit the ascii file in the vi with the following :
:%s/
for example
:%s/XXX/YYY/g (g = global - whole file)
Regards
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:03 AM
тАО09-07-2004 02:03 AM
Re: Change occurrence of a string in shell
???
Could you please a bit more specific
Regards
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:13 AM
тАО09-07-2004 02:13 AM
Re: Change occurrence of a string in shell
Now the proper solution, but it works :
CHAR="123xxx789xxx"
echo `echo $CHAR | cut -c1-3`"yyy"`echo $CHAR | cut -c7-`
Olivier.
- Tags:
- cut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:13 AM
тАО09-07-2004 02:13 AM
Re: Change occurrence of a string in shell
perl -p -e 's/^(...)XXX/$1YYY/' infile >otfile
This will only replace XXX when it is in columns 4-6.
HTH
-- Rod Hills
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:17 AM
тАО09-07-2004 02:17 AM
Re: Change occurrence of a string in shell
substr ($_, 5, 3) =~ s/XXX/YYY/;
changes XXX to YYY in any line starting at pos 5 (0 based) with length 3.
Implementing that in Rodney's script is left an excercise to the reader
Enjoy, Have FUN! H.Merijn [ who does not like counting dot's ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:26 AM
тАО09-07-2004 02:26 AM
Re: Change occurrence of a string in shell
for i in `cat file`
do
START=`expr substr $i 1 3`
echo $i | sed "s/^${START}XXX/${START}YYY/"
done
Regards,
Fred
"Reality is just a point of view." (P. K. D.)