A basic question about AND logical operator. I'm trying to extract some fields in my data file niveles.csv based on values of columns 1 and 2. I'd like to write an awk sentence saying "when field1=date and field2=area then print fields 3 to 5".
My data file looks like
01-08-12;1;0;0;0
01-08-12;2;1;1;1
01-08-12;3;0;0;1
................
awk sentence inside a bash script is
date=01-08-2012
area=8
awk 'BEGIN {FS=";"} $1 ~ /'$date'/ && $2 ~ /'$area'/ { print $1 " " $2 " " $3 " " $4 " " $5 }' niveles-rams.cs
But this just gives fields for the three cases where area number has an 8 inside (8,18 and 28) while I just want only area 8 data
01-08-2012 8 0 0 0
01-08-2012 18 2 2 1
01-08-2012 28 2 1 0
Thanks in advance for your attention, sure it is a simple question for experienced users.
<= – less than or equal to. == – equal to. !=
Not Equal to It is represented by != . It returns true if both operands are unequal, otherwise it returns false.
Awk OR Logic OR logic is used to check given conditions and if one of the conditions is true the whole or logic will return true. We will use || signs to specify OR logic. The syntax of OR logic is like below.
Not tested:
awk 'BEGIN {FS=";"} $1 ~ /'$date'/ && $2 == '$area'{......}'
$2 ~ /8/ -> this means if 2nd field contains 8
$2 == 8 -> this means if 2nd field is equal to 8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With