awk Question: How to remove lines in which $3 == $1 +4

Hi all,

I am trying to delete all lines from a file in which the value in 'column 3' is not the value of 'column 1' + 4. The code below that I tried doesn't work.

awk '$3 == $1 + 4 {print}' input > output

Example Input:-

1 xxx 2
3 xxx 26
4 xxx 8
2 xxx 9
7 xxx 11

(input file actually has 8 columns, this is just what the first 3 columns look like)

Example desired output:-

4 xxx 8
7 xxx 11

Any help would be very gratefully received!

You were very close. Try:

awk '$3 == ($1 + 4) {print}' input > output

or, more simply:

awk '$3 == ($1 + 4)' input > output

Hi,

Thank you very much for your reply. I think I also tried with the brackets you suggest but it didn't work. (I have left my office now so I cant check until tomorrow morning, I'm on UK time.)

Do you know of any reason why it might not work and any other suggestions?

Many thanks,
Liv

Note that I suggested parentheses, not brackets. But my suggestion and your original code should produce the same results with a version of awk that conforms to the standards.

What OS are you using?

You said the awk script wasn't working, but you didn't show us what output you were getting. If you would show us the output, it would make it a lot easier to evaluate what might be wrong.

If you're using a Solaris/SunOS system, try using /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of the default /usr/bin/awk . /usr/bin/awk on Solaris systems is an old version of awk that is to be used to run old scripts using a much earlier specification describing how awk should behave.

I apologise for not being more specific.

I am using Linux, bash shell.

I get no error message when implementing, however the output file I get is completely blank, although I know for sure that there are lines in the input file that match the pattern.

I use awk in otherways by just typing "awk" and it works fine.

Many thanks,
Liv

OK. Then we need to see what is actually in a few lines of your input file (including at least one line that should be selected and at least one line that should not be selected. Please also show us the output from the command:

od -bc input

for your sample input file.

Sure, I'll post again tommorrow morning when I get back to my office. Thanks for your help.

Liv

@Don CragunThat shouldnt matter as the precedence of '+' is higher than '==' so addition will be performed before the test for equality...

@OPPost an actual sample of the input file in case the 1st or 3rd columns are strings instead of numbers...

Hi shamrock,

Yes, I mentioned in message #4 in this thread that the parentheses shouldn't matter. (However, I have seen evidence that some implementations of awk don't conform to the precedence rules specified by the standards.)

And, in message #6, I asked for a sample of the input, and, in message #7, livbaddeley said a sample will be provided tomorrow.

Thank you very much, the code you helped me with works fine!