Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select lines based on value in a column

Tags:

bash

awk

I have a tab delimited table for which I want to print all lines where column 'x' is greater than 'Y'. I have attempted using the code below but am new to using awk so am unsure how to use it based on columns.

awk '$X >= Y {print} ' Table.txt | cat > Wanted_lines 

Y are values from 1 to 100.

If the input were like below with column X were the second column.

1    30
2    50
3    100
4    100
5    80
6    79
7    90

The wanted output would be:

3    100
4    100
5    80
7    90

The first 2 lines of the file is:

1   OTU1    243622  208679  121420  265864  0   0   2   0   0   11  1   5   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   839604  OTU1    -   Archaea 100%    Euryarchaeota   100%    Methanobacteria 100%    Methanobacteriales  100%    Methanobacteriaceae 100%    Methanobrevibacter  100%
2   OTU2    84366   120817  15834   74737   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   295755  OTU2    -   Archaea 100%    Euryarchaeota   100%    Methanobacteria 100%    Methanobacteriales  100%    Methanobacteriaceae 100%    Methanobrevibacter  100%
like image 449
Tom Avatar asked Sep 30 '22 16:09

Tom


1 Answers

First

awk's default internal field separator (FS) will work on space or tab delimited files.

Secondly

awk '$x > FLOOR' Table.txt

Where $x is the target column, and FLOOR is the actual numeric floor (i.e. 5000, etc ...)

Example file: awktest

500  100
400  1100
1000 400
1200 500


awk '$1 > 1000' awktest

1200   500

awk '$1 >= 1000' awktest

1000   400 
1200   500

Thus, you should be able to use a relational expression to print the lines where x > y, in the form:

awk '$x > $y' awktest

Where $x is a numeric column as in $1, or other.

Where $y is a numeric column as in $2, or other.

Example:

awk '$1 > $2' awktest

or ...

awk '$2 > $1' awktest

awk numbers are floating point numbers, so you can compare decimals, too.

like image 88
Anthony Rutledge Avatar answered Oct 03 '22 22:10

Anthony Rutledge