Re: Change occurrence of a string in shell (671 Views)
Reply
Frequent Advisor
Diego Balgera
Posts: 50
Registered: ‎07-23-2000
Message 1 of 15 (671 Views)

Change occurrence of a string in shell

Hi,
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.

Trusted Contributor
Manish Srivastava
Posts: 170
Registered: ‎05-24-2004
Message 2 of 15 (671 Views)

Re: Change occurrence of a string in shell

Hi,

If it is always the first on the line then this should work:

sed 's/XXX/YYY/' file_name

manish
Frequent Advisor
Diego Balgera
Posts: 50
Registered: ‎07-23-2000
Message 3 of 15 (671 Views)

Re: Change occurrence of a string in shell

Thank you, but it is not my case.
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.
Respected Contributor
Olivier Decorse
Posts: 345
Registered: ‎11-09-2000
Message 4 of 15 (671 Views)

Re: Change occurrence of a string in shell

If you just want to replace the first xxx :
cat file | sed -e "s/XXX/YYY/"

Olivier.
They say "install windows 2k, xp or better", so i install unix !
Respected Contributor
Franky_1
Posts: 322
Registered: ‎01-14-2001
Message 5 of 15 (671 Views)

Re: Change occurrence of a string in shell

Hi,

you cane edit the ascii file in the vi with the following :

:%s///g

for example

:%s/XXX/YYY/g (g = global - whole file)

Regards

Franky
Don't worry be happy
Frequent Advisor
Michael_356
Posts: 69
Registered: ‎09-21-2003
Message 6 of 15 (671 Views)

Re: Change occurrence of a string in shell

"Could be or not be the first occurence"

???

Could you please a bit more specific

Regards

Michael
Respected Contributor
Olivier Decorse
Posts: 345
Registered: ‎11-09-2000
Message 7 of 15 (671 Views)

Re: Change occurrence of a string in shell

ok, i understand, now.
Now the proper solution, but it works :

CHAR="123xxx789xxx"
echo `echo $CHAR | cut -c1-3`"yyy"`echo $CHAR | cut -c7-`

Olivier.
They say "install windows 2k, xp or better", so i install unix !
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 8 of 15 (671 Views)

Re: Change occurrence of a string in shell

How about with perl-

perl -p -e 's/^(...)XXX/$1YYY/' infile >otfile

This will only replace XXX when it is in columns 4-6.

HTH

-- Rod Hills
There be dragons...
Honored Contributor
H.Merijn Brand (procura
Posts: 6,188
Registered: ‎10-13-1997
Message 9 of 15 (671 Views)

Re: Change occurrence of a string in shell

Rodney, even better, substr can be an lvalue

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 ]
Enjoy, Have FUN! H.Merijn
Honored Contributor
Fred Ruffet
Posts: 1,366
Registered: ‎08-17-2003
Message 10 of 15 (671 Views)

Re: Change occurrence of a string in shell

Only if you absolutly don't want to use perl (Procura's solution's really nice).

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.)
Honored Contributor
Jean-Luc Oudart
Posts: 1,461
Registered: ‎03-10-1998
Message 11 of 15 (671 Views)

Re: Change occurrence of a string in shell

Awk solution :

#!/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
fiat lux
Honored Contributor
Rodney Hills
Posts: 1,679
Registered: ‎09-09-1996
Message 12 of 15 (671 Views)

Re: Change occurrence of a string in shell

Procura,

Cool- I didn't even think about substr being usable as an lvalue...

-- Rod Hills
There be dragons...
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 13 of 15 (671 Views)

Re: Change occurrence of a string in shell

If you want to change the first occurence of XXX with YYY then,

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
Easy to suggest when don't know about the problem!
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 14 of 15 (671 Views)

Re: Change occurrence of a string in shell

We can do checking and replacing as,
[[ "$(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
Easy to suggest when don't know about the problem!
Honored Contributor
Elmar P. Kolkman
Posts: 1,179
Registered: ‎10-16-2003
Message 15 of 15 (671 Views)

Re: Change occurrence of a string in shell

Rodney's perl solution works with sed too, of course.

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.
Every problem has at least one solution. Only some solutions are harder to find.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.