select lines with certain values on certain fields with awk

I need a awk command to select from a log-file only the lines that have on the 2nd field (considering "|" separator) one of the values 10.216.22.XX or 10.216.22.YY or 10.216.22.ZZ and on the 4th field only values that contain strictly digits. I want the command to work parsing the file only once (I don;t want pipes in the command)

I know that to select the lines with only digits in the value from 4th field I use this command :

awk -F\| '$4~/[^0-9 ]/

So I basically want to add an additional condition to this command referring to the 2nd field where I'm looking for one of the values "10.216.22.XX|10.216.22.YY|10.216.22.ZZ" . Basically this is how a line I target should look like :

20120923000000123 | 10.216.22.XX | portalPaymentFixed | 1256004 | Y |

Assuming your field separator equals " | " (space-pipe-space), try:

awk -F ' \\| ' '$2~/^10\.216\.22\.(XX|YY|ZZ)$/ && $4~/^[0-9]+$/' file
1 Like

Like a boss !