1,105,594 Community Members

SED Replace

Member Avatar
axn
Light Poster
44 posts since Sep 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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 - A.int1.int2.any.string.here

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

A...ABCD.DEFG.HIJK
A..ABCD.EFGH.IJKL
A.ABCD.
EFGH.JKLM

Member Avatar
chris.kelley.5015
Newbie Poster
6 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

A.int1.int2.ABCD.DEFG.HIJK

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: