1

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?

2
  • 1
    Are you looking at 'rows 4 and 6' or 'columns 4 and 6'? It looks as though you might mean/intend: if ($1 == 4 || $1 == 7 || $1 == 9 || ($4 <= 9000 && $6 <= 9000)). If you truly intend 'rows 4 and 6', then you need to compare variable NR with those numbers, and…well, it is ambiguous what it means when you say "those that have numbers less than 9000 in rows 4 and 6'. Fortunately, you probably don't mean this. Whatever you intend, the answer is to make sure you put enough brackets into the condition to ensure that it is unambiguous to both the humans and the computers that have to read it. Commented Mar 27, 2015 at 2:12
  • I need ($1 == 4 || $1 == 7 || $1 == 7) and ( $4 <= 9000 && $6 <= 9000) to both be true. I'm looking at columns $1, $4, and $6 for each line of data. Commented Mar 27, 2015 at 13:07

2 Answers 2

2

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}'
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.