Home > other >  I have two huge sequencefiles where i want to extract the same linenumbers from file1 in file2
I have two huge sequencefiles where i want to extract the same linenumbers from file1 in file2

Time:11-11

I have my two sequencefiles and I have a list of rows/lines of interest from file1. I want to extract the lines with the same linenumber as in file1. The list is just 1 collumn of numbers.

I tried using awk in a loop, but all I get is an empty file as outputfile. My code looks like this:

for i in <listfile>;
do awk -F lnr="$i" 'NR==lnr' <file2> > outputfile

The outputfile is created but is just empty. I could not find this question being asked before, but if so sorry for wasting your time

CodePudding user response:

If I understand the question - file 1 has a list of "line numbers" and you desire to print those lines in file2:

awk 'FNR==NR{line[$1]=1;next}{if(line[FNR]==1)print FNR, $0}' file1 file2

CodePudding user response:

Given the input...

for i in {a..z}; do echo $i; done > /tmp/list-1
for i in {z..a}; do echo $i; done > /tmp/list-2

The current line of each file will be stored in FNR, so you can use that.

$ awk -v a=4 -v b=9 'FNR >= a && FNR <= b { print FILENAME, NR, FNR, $0 }'  /tmp/list-*

Sample output:

/tmp/list-1 4 4 d
/tmp/list-1 5 5 e
/tmp/list-1 6 6 f
/tmp/list-1 7 7 g
/tmp/list-1 8 8 h
/tmp/list-1 9 9 i
/tmp/list-2 30 4 w
/tmp/list-2 31 5 v
/tmp/list-2 32 6 u
/tmp/list-2 33 7 t
/tmp/list-2 34 8 s
/tmp/list-2 35 9 r
  • Related