Pass parameter to nawk from shell script

I need to parse log files using nawk, but I'm not able to pass script input argument (date) to nawk, for example:

------------

#!/bin/ksh

read date


nawk -F, '{if($1==date) print $4" "$5}'

-------------

Is there a way to pass an argument to nawk from shell script.

Many thanks :slight_smile:

Yes... use -v option

nawk -v myarg="123" '{print myarg}' 

--ahamed

Thanks for reply, :slight_smile:

what I need is something like:

 cat file.txt | nawk -F, '{if(match($11,433)) {print $16}}'

I need to replace "433" with a variable tat can be script input.

Many thanks

nawk -F, -v regex=433 '{if(match($11,regex)){print}}' file.txt

You don't need to use cat.

--ahamed

PS : Please use code tags. Thank you for your co-operation.

1 Like

Many thnaks it works fine for me

you don't need 'match' and you don't need an explicit 'print':

nawk -F, -v regex=433 '$0 ~ regex' file.txt

Would you please tell me for what we use "~" within nawk command ?

It's used match regular expressions on input fields, in this case $0 (all input)

Ok thanks a lot (Y)