Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk string inequality seemingly not working

Tags:

bash

awk

I have a file where I want to find all lines where column three and four differ. My file looks like:

chr1:109506687  [T/G]   BOT     TOP
chr1:109506690  [T/G]   BOT     TOP
...

The code I use to find these lines is

awk '$3 != $4 {print $0}' Cardio-Metabo_Chip_11395247_A.txt | shuf -n 10

Problem is that using this command I get results like

rs3218791       [A/C]   TOP     TOP

Where column three and four are the same.

When I use the conditional for equality, namely == I get no output, which tells me that awk never considers the two columns $3 and $4 equal, despite them often being so.

Ps. using :set list in vim, my file looks like:

chr1:109506687^I[T/G]^IBOT^ITOP$
chr1:109506690^I[T/G]^IBOT^ITOP$
....

My awk version is GNU Awk 3.1.8, but I can't imagine that having to do much with anything. This should have been right in 1.0

What might be wrong?

like image 913
The Unfun Cat Avatar asked Jul 10 '26 13:07

The Unfun Cat


1 Answers

Though I can't reproduce your issue (see below), I think you're evaluating those values numerically rather than as strings (all nonempty strings —even "0"— numerically evaluate to 1). Try this:

awk '$3 != $4 "" {print $0}' test

That concatenates $4 with an empty string and should therefore force your desired string comparison.


I failed to reproduce your problem with mawk 1.2 and gawk 4.0.1:

$ cat test
chr1:109506687  [T/G]   BOT     TOP
chr1:109506690  [T/G]   BOT     TOP
rs3218791       [A/C]   TOP     TOP
$ mawk '$3 != $4 {print $0}' test
chr1:109506687  [T/G]   BOT     TOP
chr1:109506690  [T/G]   BOT     TOP
$ gawk '$3 != $4 {print $0}' test
chr1:109506687  [T/G]   BOT     TOP
chr1:109506690  [T/G]   BOT     TOP

The shuf pipe shouldn't have anything to do with it, nor should tabs vs spaces. (Though to be safe, I tried all combinations in my test.)

Fun tip: {print $0} is implied if there's only one clause with no action. Therefore, awk '$3 != $4' is the same as awk '$3 != $4 {print $0}' ... though be sure you're not making code harder for your peers to read.

like image 125
Adam Katz Avatar answered Jul 12 '26 09:07

Adam Katz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!