xI have a text in this form:
foo1 bar1 xId "myId1";yId "something"
foo2 bar2 xId "myId2";yId "something"
foo2 bar2 yId "something";xId "myId3"
How can I use sed to edit the myId field? I want to append a value before it, like this:
foo1 bar1 xId "prefix_myId1";yId "something";
foo2 bar2 xId "prefix_myId2";yId "something";
foo2 bar2 yId "something";xId "prefix_myId2";
I cannot use awk because xId is not always in the same place in my file. However, it is guaranteed that the line is in this format: someStuff xId "myContent"; someOtherStuff
Thanks a lot, I can use
sed 's/\(.*xId \)[^ ]*\(;.*\)/ a \1"newValue"\2/'
but it replaces the contents by newValue instead of prefixing it...
The capture group should make use of xId
EDITED TO SHOW TRICKY PART, THANKS FOR YOUR COMMENTS
With
sed
:I'm using two capturing groups:
([^"]*")
catches everything after thexId
before the next opening"
including them.([^"]*)
selects the content between the"
.In the replacement pattern I reassemble the groups and inject the term
prefix_
.