How to replace pattern

Hello EveryOne,

       I have a file "test" with following entry.

this is test prtestad yes this is test .

Above i have colored pr and ad .

Actually requirement is to replace all words starts with pr and ends with ad should be replaced by word prahlad .

Means after changes applied it should look like as follows.

this is test prahlad yes this is test .

Words starting with pr and ending with ad can come any where in the file , may be in the start of line or end or in middle .

Please help me with this.

Thanks in Advance.

$ echo "this is test prtestad yes this is test" | sed "s/pr[^ ]*ad/prahlad/g"         
this is test prahlad yes this is test

Dear Scottn,

Thank you very much. :slight_smile:

Could you please help me in understanding "
pr[^ ]*ad

---------- Post updated at 05:51 AM ---------- Previous update was at 05:50 AM ----------

Dear Scottn,

Thank you very much. :slight_smile:

Could you please help me in understanding "pr[^ ]*ad" completely.

Thanks,
Prahlad

cat test | sed 's/\<pr.*ad\>/prahlad/'

That won't work, since there are no "<" ">" symbols in the input.
If one removes "\<" and "\>" from the sed command it will work as desired.
Btw. it looks like another useless use of cat :stuck_out_tongue:

In here probaly is the goal that must be used for mark word boundaries..
But it is unnecessary :slight_smile:

For example

# echo "this is test prtestad yes this is test" | sed 's/\<pr....ad\>/prahlad/'
this is test  prahlad  yes this is test
# echo "this is test prtestad yes this is test" | sed 's/pr....ad/prahlad/'
this is test  prahlad  yes this is test

Still doesn't work for me...

$ echo "this is test prtestad yes this is test" | sed 's/\<pr.*ad\>/prahlad/'
this is test prtestad yes this is test
$ 
$ echo "this is test prtestad yes this is test" | sed 's/\<pr....ad\>/prahlad/'
this is test prtestad yes this is test
$

Maybe it expects the GNU version of sed :confused:

Scottn,

 
echo "this is test rrprdddddad prtestad yes this is test" | sed 's/pr[^ ]*ad/prahlad/g'
this is test rrprahlad prahlad yes this is test

I would prefer to add spaces , like below so only the "Words" starting and ending with the pr and ad will be modified

 
echo "this is test rrprdddddad prdsdjsnadjjdsn prtestad yes this is test" | sed -e 's/ pr[^ ]*ad$/ prahlad/g'  -e 's/^pr[^ ]*ad /prahlad /g' -e 's/ pr[^ ]*ad / prahlad /g'
this is test rrprdddddad prdsdjsnadjjdsn prahlad yes this is test

Which version your sed ? v.4.x?

All right, I launched DSL (damn small linux) as Qemu guest and run the GNU version of sed (v4.0) and it worked.
So next time I will check that before I report a non-working sed command :rolleyes: