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)
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?
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 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.
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:
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.