Replace exact word by perl

my inputfile:

This is a test and only a test for production - prod.

echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g'

outputfile:

This is a test and only a test for testuction - test

I only want to replace prod, not production.

I also tried to use sed:

echo "This is a test and only a test for production - prod" | sed -e 's/prod/test/g'
This is a test and only a test for testuction - test
 

Thank you in advance for helping.

This highly depends on e.g. the sed version you are running. With mine(sed (GNU sed) 4.2.2), this works:

echo "This is a test and only a test for production - prod" | sed -e 's/\<prod\>/test/g'
This is a test and only a test for production - test
1 Like

I found this on unix.com

sed 's/\<apple\>/XXX/g' filename

and solved my sed

I am digging for perl. Again, thank you.

You can try:

perl -pi -e 's/\bprod\b/test/g'
1 Like