greg
greg

Reputation: 47

Adding a String in front of/right after a match via 'sed'

I have a bunch of files with Strings I want to search for a specific match: "exampledata"

Looks like this:

...(...name: Example; age: 21; version: 2131; exampledata: hello;) end

if in a line the "exampledata" is defined then do nothing, otherwise add that string exampledata with lets say a number right after the version number (which could be 1 cipher or up to 5-6) OR put it right before the closing bracket ")" which is always the pattern in the Strings. (i.e. "Exampledata" is right after version and at the end of the Strings. Only a ")" comes after it.)

sed '/exampledata:"/b; /\version.....;\/s/&"exampledata: 2"/'

this is what i worked out so far, i dont know how to make the dots after version interactive bc. in every line there could be a single cipher version or a higher number. ideas?

Basically im looking for a sed command that adds an "exampledata: 2;" if there was no exampledata at all before for instance? (result would be like this: blabla...(...version: 98; exampledata: 2;)

Upvotes: 0

Views: 119

Answers (1)

Barmar
Barmar

Reputation: 782508

Your patterns have double quotes that don't appear in the data.

sed '/exampledata:/!s/version: [0-9]*;/& exampledata: 2;/'

/exampledata:/! makes the command execute on lines that don't contain exampledata:, and then it performs a more correct substitution.

Upvotes: 1

Related Questions