I am trying to append _out
to anything that matches the regex shown in the follwing sed command. The _out
should be before the [ (]
.
sed -n '/[^_]lm[01?]_.*[ (]/p'
The command returns the lines correctly as I expect. Now the problem comes when I try the following command where I define regions to use in the replacement.
So for example if I have a match of lm?_whatever(
it should be replaced by lm?_whatever_out(
.
I used
sed 's/([^_]lm[01?]_.*)([ (])/1_out2/g' text_to_edit
And as you might guess I was unlucky. What am I doing wrong?
EDIT:
Here is an example explaining more:
The text:
this will make absolutely no sense=lm?_hello <= four_lm1_hi(5:0) bla bla lm1_blabbering(5:0)
should turn into
this will make absolutely no sense=lm?_hello_out <= four_lm1_hi(5:0) bla bla lm1_blabbering_out(5:0)
Advertisement
Answer
In order to only match your lm
words and not affect the following ones, you need to restrict the pattern. I suggest turning .*
to [^[:blank:]]*
to match 0 or more characters other than spaces or tabs:
sed 's/([^_]lm[01?]_[^[:blank:]]*)([ (])/1_out2/g' text_to_edit
(EDIT) Or, if the only characters there are between _
and are alphanumeric characters, you may use
[[:alnum:]]
character class:
sed 's/([^_]lm[01?]_[[:alnum:]_]*)(.)/1_out2/g' text_to_edit
This way, we’ll make sure only the lm?_hello
and lm1_blabbering
are matched.