1

Here is another solution quite easy:
grep -o "[tc,ud]*p\\/.* " INPUTFile.txt | awk '{print $1}'
grep
doesn't match anything. [tc,ud]\*\\/.*
looks for one occurrence of either t
, or c
, or ,
or u
or d
, followed by a literal *
character, then a p
and a backslash. You probably meant grep -Eo '(tc|ud)p/.* ' file | awk '{print $1}'
. But then, if you're using awk, you may as well do the whole thing in awk: awk -F'[= ]' '/(tc|ud)p/{print $2}' file
. [tc,ud]p
means "one of t
, c
, ,
, u
or d
followed by a p
. So it matches here only because tcp
has cp
and udp
has dp
. But it would also match ,p
or tp
etc. Also, now that you have the *
, it will match ppp
as well (the *
means "0 or more" so it will match even when it doesn't match). You don't want a character class ([ ]
), what you want is a group: (tc|ud)
(use with the -E
flag of grep
). Also, the .*
makes it match the entire line. \*
to get the first *
in their command to appear as an * and not as italics markdown. When you put the command into code format, you caused the \
before the *
to appear (thus causing the command to fail). When you edit other people’s posts, please watch out for changing the appearance of the post like this. ppp
. Of course you’re right that it will match ,p
or tp
— or uucp
, ttp
, cutp
, ductp
or d,up
. (2) I have been banging my head against the wall for years over questions where the question describes a problem, and then gives an example of the input, and people post answers that work for example, but not the general problem, as described. For example, most of the answers here — including yours — … (Cont’d) proto=
is the first thing on the line, and that the proto value is always tcp/something
or udp/something
. Kusalananda offers an answer that assumes that something
matches [[:alnum:]]*
(counterexamples: ftp-data
is a standard name for port 20; netbios-ns
, netbios-dgm
, netbios-ssn
, dhcpv6-client
and dhcpv6-server
are also standard service names). … (Cont’d) /
) matches ..p
; Wikipedia offers counterexamples DCCP
, RSVP
,·SCTP
and SPX
. Freddy’s answer assumes that the proto
will be the only value on the line that contains a /
. … (Cont’d) proto=
anywhere on the line, and match/extract the following string of printing characters. By that standard, user000001’s answer is the only one that’s right. But the community seems to accept a lower standard. By that standard, mkzia’s answer is fine, because it works for the provided input data. Sure, it reflects a misunderstanding of how […]
works, and it could be reduced to [tucd]*p
, but that really isn’t any worse than ..p
. … (Cont’d) proto
as the first word. So why provide solutions where proto
isn't the first word? tcp/http, tcp/https, udp/dns.