1

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

topo lino
  • 17
  • 3

2 Answers2

2

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

grep -oE '\[(GET|POST|OPTIONS|PUT|DELETE)\]' myfile.txt
Toto
  • 89,455
  • 62
  • 89
  • 125
  • thanks for your answer. I kind of want something like this, but this command doesn't print only the method. it prints many columns, and I can't understand why – topo lino Mar 10 '20 at 13:58
  • @topolino: Please, edit your question and add some input lines and expected result. – Toto Mar 10 '20 at 14:11
0

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
luciole75w
  • 1,109
  • 6
  • 12