Operating System - HP-UX
1748158 Members
4045 Online
108758 Solutions
New Discussion юеВ

Re: Change occurrence of a string in shell

 
Diego Balgera
Frequent Advisor

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.

14 REPLIES 14
Manish Srivastava
Trusted Contributor

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
Diego Balgera
Frequent Advisor

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.
Olivier Decorse
Respected Contributor

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 !
Franky_1
Respected Contributor

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
Michael_356
Frequent Advisor

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
Olivier Decorse
Respected Contributor

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 !
Rodney Hills
Honored Contributor

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...
H.Merijn Brand (procura
Honored Contributor

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
Fred Ruffet
Honored Contributor

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.)