Append a searched string with another string using sed

Hi,

I need to replace and append a string in a text if grep is true. For eg:

grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ?

How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be,
lx123
web222
xyz

Want to replace all the above to become
lx123.abc.com.
web222.abc.com.
xyz.abc.com.

Pls. help. Thanks.

Please post the contents of test.txt and mention what you need to do to get the final/required output along with the required output. This way you might get a better solution than the unnecessary pipeline.

The Text is as follows,

mars            IN      A       202.27.17.78
sentosa         IN      A       202.27.17.100
www             IN      CNAME   mars
majordomo       IN      CNAME   mars
smtp2           IN      CNAME   sentosa
smtp3           IN      CNAME   venus.test.com.

Script suppose to grep for CNAME without the "." at the end and append them with .test.com. so that the above will become,

mars            IN      A       202.27.17.78
sentosa         IN      A       202.27.17.100
www             IN      CNAME   mars.test.com.
majordomo       IN      CNAME   mars.test.com.
smtp2           IN      CNAME   sentosa.test.com.
smtp3           IN      CNAME   venus.test.com.

Thanks.

awk -v OFS="\t" '!/\.$/&&$NF!~/^[0-9]/{$NF=$NF".test.com."}1' input.txt
awk -v VM=".test.com." '/CNAME/{if($NF !~ VM){$NF=$NF""VM}}1' OFS="\t" file

How do I change it to apply to only lines with "CNAME"? Will grep work? Thanks.

see my post above..:slight_smile:

 
awk -v OFS="\t" '$3~/CNAME/&&$NF!~/\.$/{$NF=$NF".test.com."}1' input.txt

Yes just saw it, tested and working. Thanks all for the help :slight_smile: