Subhaa Chandar Subhaa Chandar - 1 month ago 6
Linux Question

Replacing end of word with something else

I have this:

echo "walking ringer talking"


How do I use sed to achieve the following output?

walked ringer talked


I've tried several ways, but can't get it to work. The following commands are what I've tried.

echo "walking ringer talking" | sed 's/ing/ed/g'


This outputs
walked reder talked
.

echo "walking ringer talking" | sed 's/ing$/ed/g'


This outputs
walking ringer talked
.

echo "walking ringer talking" | sed 's/^ing/ed/g;s/ing$/ed/g'


This outputs
walking ringer talked
.

I'm trying to get this done in a bash script.

Answer

add space 's/ing /ed /g; in your third test;

#echo "walking ringer talking" | sed 's/^ing/ed/g;s/ing$/ed/g' 
echo "walking ringer talking" | sed 's/ing /ed /g;s/ing$/ed/g'

eg:

user@host:/tmp/test$ echo "walking ringer talking" | sed 's/ing /ed/g;s/ing$/ed/g' 
walked ringer talked
Comments