0

I have a file try.txt that looks like this:

(SOME_PRINT): [a] content
(SOME_PRINT): [a] [b] content

If I have this pattern in the beginning of a line in the file: (SOME_PRINT): [<word>] (where <word> is a combination of letters and numbers only), so I would like to replace it with (OTHER_PRINT):.
For the file above, I would like to find a command that its execution will make the file become:

(OTHER_PRINT): content
(OTHER_PRINT): [b] content      

I tried to run sed -r -i 's/^\(SOME_PRINT\)\: \[.*\] /\(OTHER_PRINT\)\: /' try.txt , and got this output:

(OTHER_PRINT): content
(OTHER_PRINT): content               

Can you explain why has [b] disappeared?

John
  • 861
  • 1
  • 7
  • 19
  • note that sed doesn't support non-greedy, but as specified in answers of the duplicate question, you can use the workaround in this case.. also, you can avoid having to escape the `()` by removing the `-r` option – Sundeep Dec 14 '17 at 16:02

1 Answers1

1

Just change your regex like this:

sed -r -i 's/^\(SOME_PRINT\)\: \[[a-zA-Z0-9]\] /\(OTHER_PRINT\)\: /' try.txt

Your error is that you this part of your regex is not precise enough: \[.*\]

[a] [b] can be catch by \[.*\]

On solution is to specify only characters that can be found between your square brackets. As you told about only alphanumeric characters, this can be done by this selector: [a-ZA-Z0-9].

Another solution is to exclude the closing square bracket from the characters.

I often try my regex with https://regex101.com/. It's very cool and comprehensible.

Hadrien Huvelle
  • 382
  • 2
  • 11