- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- 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
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
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2004 02:40 AM
09-07-2004 02:40 AM
Re: Change occurrence of a string in shell
#!/bin/sh
awk '{
if(substr($0,4,3)=="XXX") {
printf("%s%s%s\n",substr($0,1,3),"YYY",substr($0,7));
} else print;}'
or shorter
awk '{ if(substr($0,4,3)=="XXX") printf("%s%s%s\n",substr($0,1,3),"YYY",substr($
0,7)); else print; }'
Regards,
Jean-Luc
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2004 02:46 AM
09-07-2004 02:46 AM
Re: Change occurrence of a string in shell
Cool- I didn't even think about substr being usable as an lvalue...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2004 05:06 AM
09-07-2004 05:06 AM
Re: Change occurrence of a string in shell
echo "123XXX789XXX" | sed -e 's/XXX/YYY/1'
or
echo "123XXX789XXX" | sed -e 's/XXX/YYY/'
It will only change the first XXX with YYY
To change the next occurence with YYY then,
echo "123XXX789XXX" | sed -e 's/XXX/YYY/2'
To all,
echo "123XXX789XXX" | sed -e 's/XXX/YYY/g'
Regards
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2004 06:50 PM
09-07-2004 06:50 PM
Re: Change occurrence of a string in shell
[[ "$(echo "123XXX789XXX" | cut -c 4-6)" = "XXX" ]] && echo "123XXX789XXX" | perl -pe 's/^(...)XXX/$1YYY/'
or as,
VAR="123XXX789XXX"
[[ "$(echo $VAR | cut -c 4-6)" = "XXX" ]] && VAR=$(echo $VAR | perl -pe 's/^(...)XXX/$1YYY/')
echo $VAR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2004 07:07 PM
09-07-2004 07:07 PM
Re: Change occurrence of a string in shell
sed 's|^\(...\)XXX|\1YYY|' file
As for the remark 'can be the first occurrence or not' I think Diego meant that it is possible that there are X characters in the first 3 characters of the line, resulting in problems with the first two solutions.