Operating System - HP-UX
1827791 Members
2453 Online
109969 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.)
Jean-Luc Oudart
Honored Contributor

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

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...
Muthukumar_5
Honored Contributor

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!
Muthukumar_5
Honored Contributor

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!
Elmar P. Kolkman
Honored Contributor

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.