sed - need to replace only the chars or symbol after 1st and 2nd dot(.) with "int1" and "int2" respectively.
Here is what I have tried but it replaces the strings in other lines.

End result should look like -

sed -e 's//int1/' -e 's//int2/' -e 's/ABCD/int1/' filename


sed uses the 's///' command to replace one string with another. You need to choose the string to be replaced and put it in the first space between the first two forward slashes and put the new string between the second two.

In this case, knowing regular expressions will help. Test with the folowing:

echo "A...ABCD.DEFG.HIJK" | sed -e 's/\([A-Z]\)\./\1\.int1\./' -e 's/\.\./\.int2/'

The output is:


It might be easier if you choose a different character to divide up the sections of the replacement command for sed:

echo "A...ABCD.DEFG.HIJK" | sed -e 's_\([A-Z]\)\._\1\.int1\._' -e 's_\.\._\.int2_'

That way you can see which items are being removed and which are being added in.

I know that the above code is a little cumbersome. I am a great believer in elegance in simplicity. However, I am still learning, and this is what I know at this point. I welcome corrections toward simplicity.

This article has been dead for over six months. Start a new discussion instead.