0

I came across below statement here:

To include ] in the list of characters matched by a bracket expression, make it the first character (or first after ^ for a negated set): []abc] or [^]abc] (not [abc]] nor [abc\]]).

So I tried out kind of below border case of matching and replacing both [ and ]:

echo '[]' | sed 's/[[]/^/'      #only [ in matching string (works)
echo '[]' | sed 's/[[]]/^/'     #both [] in matching string (fails #1)
echo '[]' | sed 's/[\[\]]/^/'   #escaping both [] in matching string (fails #2) 
echo '[]' | sed 's/[][]/^/'     #including ] as first character (as suggested by above quoted point) inside [..] leads to [][] pattern (fails #3)
echo '[]' | sed 's/[]]/^/'      #only ] in matching string (works)

Output was:

^]
^
^
^]
[^

In failures above, I felt it should have printed ^^, that is, replacing both square brackets in [] with ^. I have numbered failures #1, #2 and #3.. Can someone please explain the reason behind the output I am getting in each of these failures and what should be the way to make it work when I want to match both [ and ]?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • 4
    `echo '[]' | sed 's/[][]/^/g'` works. It says, do that substitution `g`lobally (otherwise, sed just does the first replacement on that line and stops trying. – jeremysprofile Sep 03 '20 at 23:04
  • 4
    Without the `g` flag, only the first match is replaced. – Wiimm Sep 03 '20 at 23:10
  • So `s/[][]/^/g` is the only correct one? Does it interprete first `[` and fourth `]` brackets for defining character set and 2nd `]` and 3rd `[` to be part of that character set? Also the output is still unchanged for failures `#1` and `#2`. Why we get this output for failures `#1` and `#2` – Mahesha999 Sep 04 '20 at 09:13
  • `<<<'[]' sed 's/[/X/'` will fail with `sed: -e expression #1, char 6: unterminated s' command` where as `s/\[/X/` will return `X]`. `s/]/X/` will return `[X` thus `s/[[]]/X/` returns `X` because `[[]` matches `[` and `]` matches `]` thus the `[]` 2 characters are replaced by `X`. Also `s/[][]/X/`will return `X]` whereas `s/[][]/X/2` returns `[X`. The gnu sed has a `--debug` option which may throw more light on the subject. – potong Sep 04 '20 at 10:31

0 Answers0