Hide part of match from grep output
Instead of using extended-regex grep (-E
), use perl-regex grep instead (-P
), with a lookbehind and lookahead.
$ grep -oP "(?<=\[)[0-9].+(?=\])" logfile
113a6d9e-7b06-42c6-a52b-7a4e4d2e216c
113a6d9e-7b06-42c6-a52b-7a4e4d2e216c
Here, (?<=\[)
indicates that there should be a preceding \[
, and (?=\])
indicates that there should be a following \]
, but not to include them in the match output.
$ cat a.txt
test hello..[113a6d9e-7b06-42c6-a52b-7a4e4d2e216c]... this is
te [113a6d9e-7b06-42c6-a52b-7a4e4d2e216c]. this is hello
$ grep -oP '(?<=\[)[^\]]*' a.txt
113a6d9e-7b06-42c6-a52b-7a4e4d2e216c
113a6d9e-7b06-42c6-a52b-7a4e4d2e216c
https://stackoverflow.com/a/19242713/6947646
sed is more applicable for the case than grep
sed '/\n/{P;D;};s/\[/\n/;s/\]/\n/;D' log