topo lino
topo lino

Reputation: 17

grep with regex printing only match

0

I made my regex and it works perfectly on my regex making tool. But when I try to use my regex in my command line it doesn't work, or at least it seems I cannot use that.

my regex is:

.*\[(GET|POST|OPTIONS|PUT|DELETE)\].*

it matches the call method no matter where it is I want it to be displayed in a simple file containing all calls.

i tried many commands, one of which is:

cat myfile.txt |  awk /'\[(GET|POST|OPTIONS|PUT|DELETE)\]/ {print $1}'

but it returns me the first column, although the matching group 1 is the method.

could anyone help me please?

I can provide an example if needed.

examples:

two possible inputs:

32.45.53.01, 32.32.32.543, 21.32.54.675, 21.32.54.779 161.21.34.56 [10/Mar/2020:13:04:14 +0100] [HTTP/1.1] [GET] [-://yahoo.com/webapp/wcs/stores/servlet/libero/home] 302 - EL=[22ms, -] WAS=[mediaworld_b2c:3213] - WAS=[-] - - [spam]

10.40.23.483 10.8.21.321 [10/Mar/2020:15:18:06 +0100] [HTTP/1.1] [GET] [-://google-preprod.test.com/sda/v1/ticrcv/TSY-JKidsahjsdaAO-A-JYVS5gGFxZ8PY8J-GRs0g-GOB2C] 200 108 EL=[17ms, -] WAS=[-] - -

and the output in both case has to be just:

GET

Upvotes: 1

Views: 1184

Answers (2)

luciole75w
luciole75w

Reputation: 1117

The answer from Toto seems to fit your needs. So for information, in awk $1 refers to the first input field on the line (as dawg pointed out) and back references are not supported. However if you have GNU awk then you can use the match function which supports an additional parameter to store the captures this way :

awk 'match($0, /\[(GET|POST|OPTIONS|PUT|DELETE)\]/, a) { print a[1] }' myfile.txt

Upvotes: 0

Toto
Toto

Reputation: 91385

Not sure I well understand, but is that what you want?

grep -oE '\[(GET|POST|OPTIONS|PUT|DELETE)\]' myfile.txt

Upvotes: 2

Related Questions