Search code examples
bashshellnawk

An error with asterisk in if statement


I'm having problem with the following code:

nawk -F "," '{if($2<=2)&&($9!=45)&&($11==2348*)) print $2}' abc12* | wc -l

The error is in ($11==2348*). I tried to put this number in variable x and do ($11==$x*).


Solution

  • if you mean a regex match change it to

    $ awk -F, '$2<=2 && $9!=45 && $11~/^2348/ {c++; print $2} END{print c}' abc12*
    

    note that you can incorporate line count in the script as well.

    If you want equality check $11=="2348*" would do. Will check that the field is literally 2348* without any special meaning of *.