With grep -o
, you will have to match exactly what you want to extract. Since you don't want to extract the proto=
string, you should not match it.
An extended regular expression that would match either tcp
or udp
followed by a slash and some non-empty alphanumeric string is
(tcp|udp)/[[:alnum:]]+
Applying this on your data:
$ grep -E -o '(tcp|udp)/[[:alnum:]]+' file
tcp/http
tcp/https
udp/dns
To make sure that we only do this on lines that start with the string proto=
:
grep '^proto=' file | grep -E -o '(tcp|udp)/[[:alnum:]]+'
With sed
, removing everything before the first =
and after the first blank character:
$ sed 's/^[^=]*=//; s/[[:blank:]].*//' file
tcp/http
tcp/https
udp/dns
To make sure that we only do this on lines that start with the string proto=
, you could insert the same pre-processing step with grep
as above, or you could use
sed -n '/^proto=/{ s/^[^=]*=//; s/[[:blank:]].*//; p; }' file
Here, we suppress the default output with the -n
option, and then we trigger the substitutions and an explicit print of the line only if the line matches ^proto=
.
With awk
, using the default field separator, and then splitting the first field on =
and printing the second bit of it:
$ awk '{ split($1, a, "="); print a[2] }' file
tcp/http
tcp/https
udp/dns
To make sure that we only do this on lines that start with the string proto=
, you could insert the same pre-processing step with grep
as above, or you could use
awk '/^proto=/ { split($1, a, "="); print a[2] }' file
sed
,awk
orperl
, notgrep
. – OrangeDog Jun 10 '19 at 15:11