user7249622
user7249622

Reputation: 115

filtering in a text file using awk

i have a tab separated text file like this small example:

chr1    100499714   100499715   1
chr1    100502177   100502178   10
chr1    100502181   100502182   2
chr1    100502191   100502192   18
chr1    100502203   100502204   45

in the new file that I will make:

1- I want to select the rows based on the 4th column meaning in the value of 4th column is at least 10, I will keep the entire row otherwise will be filtered out.

2- in the next step the 4th column will be removed. the result will look like this:

chr1    100502177   100502178
chr1    100502191   100502192
chr1    100502203   100502204

to get such results I have tried the following code in awk:

cat input.txt | awk '{print $1 "\t" $2 "\t" $3}' > out.txt

but I do not know how to implement the filtering step. do you know how to fix the code?

Upvotes: 3

Views: 45

Answers (2)

karakfa
karakfa

Reputation: 67557

here is another, might work better if you have many more fields

$ awk '$NF>=10{sub(/\t\w+$/,""); print}' file

Upvotes: 0

Anton Malyshev
Anton Malyshev

Reputation: 8871

Just put the condition before output:

cat input.txt | awk '$4 >= 10 {print $1 "\t" $2 "\t" $3}' > out.txt

Upvotes: 2

Related Questions