lsterzinger
lsterzinger

Reputation: 717

Using an if statement in awk in a bash script

I'm writing a program that takes in 'Radiosonde Weather Balloon' data and writes it to a new file.

The data is structured in a way that there are 7 columns. I'm only interested in rows that have 4, 7, or 9 in the first column, and also have numbers less than or equal to 9000 in both columns 4 and 6.

This is what I have written:

awk '{if ($1 == 4 || $1 == 7 || $1 == 9 && $4 <= 9000 && $6 <= 9000) print $2/10,$3/1e3,$4/10,$5/10,$6,$7/10}' $1 > sonde_tv

My output file only has the lines that have 4, 7, or 9 in the first position, but it will still have data over 9000 in columns 4 and 6.

What am I doing wrong?

Upvotes: 1

Views: 59

Answers (2)

Jotne
Jotne

Reputation: 41446

You can do it like this:

awk '$1~"^(4|7|9)$" && $4 <= 9000 && $6 <= 9000) {print $2/10,$3/1e3,$4/10,$5/10,$6,$7/10}' file

or like this:

$1~"^[479]$"

But this is not a good solution if you have numbers with more digits.

Upvotes: 0

John1024
John1024

Reputation: 113814

You need to group together the 'or' statements:

awk '{if (($1 == 4 || $1 == 7 || $1 == 9) && $4 <= 9000 && $6 <= 9000) print $2/10,$3/1e3,$4/10,$5/10,$6,$7/10}'

Upvotes: 2

Related Questions