An error with asterisk in if statement

400 Views Asked by At

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*).

3

There are 3 best solutions below

6
karakfa On BEST ANSWER

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 *.

2
rpy On

Looks like you intend to use regexp?

$11==2348* 

should give you a syntax error as

2348*

is an incomplete multiplication.

For a regular expression match you would have to use

$11 ~ /2348*/

if you intend to have zero to man "8"s or

$11 ~ /2348.*/ or may be $11 ~ /2348[0-9]*/

if the intial intent is having any character or only digits after "2348"

0
Sir. Hedgehog On

i think your code would work just fine if you wouldnt have added one more ")" than expected. if you count them you have 7.... so this ($11==2348*)) should acctually be ($11==2348*)