Vonton
Vonton

Reputation: 3382

awk compare two files -erase row from second file from condtion of first file

I need some help.

first file

0.5
0.4
0.1
0.6
0.9

second file .bam (I have to use samtools view)

aaaa bbbb cccc 
aaab bbaa ccaa
hoho jojo toto
sese rere baba
jouj douj trou

And I need output:

aaaa bbbb cccc
aaab bbaa ccaa
sese rere baba

Condition: if $1 from first file is in <0.3;0.6> print same row from the second file, if it is not, erase it. I want to get filtrate second file from condition of first file. I prefer awk or bash code, but It is not important.

condition for the first file:

awk '{if($1>0.3 && $1<0.6) {print $0}}'

Please could you help me? Thanks a lot

Upvotes: 2

Views: 97

Answers (3)

Jotne
Jotne

Reputation: 41460

Here is one awk solution:

awk 'FNR==NR {a[NR]=$1;next} a[FNR]>0.3 && a[FNR]<0.6' firstfile secondfile
aaaa bbbb cccc
aaab bbaa ccaa

sese rere baba is not printed since you say <0.6 and not <=0.6

Upvotes: 4

user3442743
user3442743

Reputation:

Another way

paste file1 file2 | awk '$1<=0.6&&$1>=0.3{$1="";print substr($0,2) }'

Upvotes: 5

Birei
Birei

Reputation: 36282

You can use and its getline function. It reads lines from second file, and for each one use getline to read one from first one, compare its number and print if it matches:

awk '
    BEGIN { f = ARGV[2]; --ARGC } 
    { 
        getline n <f
        if ( (n >= 0.3) && (n <= 0.6) ) { 
            print $0 
        } 
    }
' file2 file1

It yields:

aaaa bbbb cccc
aaab bbaa ccaa
sese rere baba

Upvotes: 4

Related Questions