Warm tip: This article is reproduced from serverfault.com, please click
awk

AWK regex to get month date

发布于 2020-11-29 17:27:05

I have the following strings which I like to filter by the second and third column.

23/10/2020,15/09/2020,8067363,suv,0,0,0,0,0,0,0,0,0,0
23/10/2020,22/10/2020,08067375,suv,0,0,1,0,0,0,0,0,0,0
23/10/2020,09/09/2020,8067387,suv,0,0,0,0,0,0,0,0,0,0
23/10/2020,21/10/2020,08067399,suv,0,0,0,0,0,0,0,0,0,0
23/10/2020,09/09/2020,8067417,suv,0,0,0,0,0,0,0,0,0,0

The second column is the date, which I only want the lines starting in October (24/10/20). On the other side I want to filter by the third column, which I've already done and it works on its own (tested). My question is that I want to put all this together in one line but I keep getting many errors due to syntax and perhaps regex.

What I am trying to construct is something like this:

awk ' {if ((/(\/d{1,2})\/(\/d{1,2})\/(\/d{1,2})/ {$2 = 10}) && ($3 ~ /^08/) {print}}' test.csv

Where {$2 > 10} should be the regex getting the second group (month) and evaluating to equal to 10. The second part is the third column starting by 08 which works well.

The expected output is those lines whose second column is October and third column starts by "08".

For example:

27/01/2020,24/10/2020,08077081,suv,0,0,0,0,0,2,0,0,0,0
27/05/2020,15/10/2020,08077082,suv,0,0,0,0,0,2,0,0,0,0
27/08/2020,12/10/2020,08077083,suv,0,0,0,0,0,2,0,0,0,0

Can you please help me to make this line correct?

Thank you

Questioner
js352
Viewed
0
Ed Morton 2020-11-30 02:28:47
$ awk -F'[/,]' '($5 == 10) && ($7 ~ /^08/)' file
23/10/2020,22/10/2020,08067375,suv,0,0,1,0,0,0,0,0,0,0
23/10/2020,21/10/2020,08067399,suv,0,0,0,0,0,0,0,0,0,0