-2

I need something similar to : sed: Replace part of a line

I have an IPaddres that as the pattern, I need to replace string before it.

Sample :

#stuff SSIPaddress

Needs to be

stuff SIPaddress

Ideas or at least how to turn the one from the link to work for me :)

Community
  • 1
  • 1
  • If you have tabular data, you might find that awk is a better tool for this kind of task. – Raedwald Mar 03 '16 at 18:11
  • 1
    You just want to replace `#stuff SS` with `stuff S` ? – SLePort Mar 03 '16 at 18:31
  • The question is too ambiguous without several concrete example lines of things that should or should not change. I don't think it's as complicated as you suggest in your answer, but it's hard to tell. – Joshua Goldberg Nov 20 '20 at 19:17

2 Answers2

0

Based on your example :

sed 's/^#stuff SS\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)\(.*\)/stuff S\1\3/' <<< "#stuff SS1.2.3.4"

outputs :

stuff S1.2.3.4
SLePort
  • 15,211
  • 3
  • 34
  • 44
0

CodeGnome, you're right, but let me show what I tried:

sed: Replace part of a line -this replaces after a pattern.

I tried to reverse the string to work for me ( replace after pattern, right )

echo “`grep "SS" file | sed -re '/([0-9]{1,3}\.){4}/p' 
| grep -v drama`” | rev | sed -i …|rev

Problem with this idea is, it prints a duplicate of the matched line back in the file && cant double pipe sed -i without a file at the end ( err sed: no input file)

The winning code, after a lot of sed madness

sed -ri 'drama/! { /SS/ s/SS/S/;s/#// } '  file

due to

#*.* SSdrama:123
#*.* SSIP

" Do not match drama, in a line with SS pattern, for all else ( only one ocurrence in my sample) sed replace SS with S and # with '';Note that I didn't have to match the IP at the end at all.

Community
  • 1
  • 1