The pattern *:
will match everything until the last :
. To stop at the next :
you need *?:
. E.g.:
% grep -Poz '^:MENU1\K[\w\W]*?:' data.txt
0. public
1. admin
2. webmail
:
You can strip the first line by matching the newline before your \K
. E.g.:
% grep -Poz '^:MENU1\n\K[\w\W]*?:' data.txt
0. public
1. admin
2. webmail
:
To eat the empty line and the :
you can match and discard that text. E.g.:
% grep -Poz '^:MENU1\n\K[\w\W]*?(?=\n+:)' data.txt
0. public
1. admin
2. webmail
next we can simplify your character class, to match on anything but :
:
% grep -Poz '^:MENU1\n\K[^:]*?(?=\n+:)' data.txt
0. public
1. admin
2. webmail
And finally we can rewrite the initial part of the match:
% grep -Poz '(?<=:MENU1\n)[^:]*?(?=\n+:)' data.txt
0. public
1. admin
2. webmail
This is similar to what @terdon came up with, but this takes care of the blank lines without another call to grep.
This final regex makes use of look-around assertions. The (?<=pattern)
is a look-behind
assertion that lets you match the pattern
but not include it as part of the output. The (?=pattern)
is a look-ahead
assertion and lets us match on the trailing pattern without including it in the output.
You can set a variable to hold the return code, then negate the variable before quitting:
netstat -nap |
grep "LISTEN\b" |
awk '$4 ~ /:80$/ {rc = 1; print $NF}; END { exit !rc }'
If you don't need \b
, then you can remove grep
part:
netstat -nap | awk '/LISTEN/ && $4 ~ /:80$/ {rc = 1; print $NF}; END { exit !rc }'
Best Answer
The GNU
awk
manual (sec. 3.5) documents that the regex\<
is gawk-specific and thus one should not expect it to work in other implementations.According to
man mawk
, if you place a backslash in front of a nonspecial character, then the backslash is removed. Thus, undermawk
,\<
is interpreted simply as an angle bracket character.Examples
I simplified the regex to provide examples of the different behavior:
Again,
gawk
interprets\<
as the beginning of a word whilemawk
interprets it simply as an angle bracket.What does POSIX say about this issue
The GNU
awk
manual explains:In other words, in this case, the different
awk
interpreters are free to make their own decisions.