Search code examples
linuxawkbioinformatics

print rows by matching order from other file


file1.txt

SRR1071717
SRR1079830
SRR1081765
SRR1085402
SRR1086236
SRR1092208
SRR1093930
SRR1097296
SRR1099957
SRR1120296

file2.txt

SRR1120296,female
SRR1099957,male
SRR1097296,female
SRR1093930,female
SRR1092208,female
SRR1086236,female
SRR1085402,male
SRR1081765,male
SRR1079830,male
SRR1071717,male

Desired output

SRR1071717  male
SRR1079830  male
SRR1081765  male
SRR1085402  male
SRR1086236  female
SRR1092208  female
SRR1093930  female
SRR1097296  female
SRR1099957  male
SRR1120296  female

I want to maintain the order of file1.txt to print file2.txt. Both files have equal rows (can't use sort because order will be changed).


Solution

  • maybe try

    awk -v FS="," 'FNR==NR {a[$1]=$2; next}; {if ($1 in a) print $1,a[$1]}' file2.txt file1.txt
    

    FNR==NR means first file

    a[$1]=$2 is like a[SRR1120296]=female

    second file if SRR1120296 is index in array a then print SRR1120296 and array value