Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk AND operator

Tags:

awk

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.

like image 985
pacomet Avatar asked Oct 25 '12 11:10

pacomet


People also ask

What is == in awk?

<= – less than or equal to. == – equal to. !=

Is not equal to in awk?

Not Equal to It is represented by != . It returns true if both operands are unequal, otherwise it returns false.

Can I use or with awk?

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.


1 Answers

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

like image 64
Guru Avatar answered Oct 09 '22 13:10

Guru