简体   繁体   中英

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?

您需要将“或”语句组合在一起:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM