I'm trying to sort the rows of a text file based on an 'index' in another file, such that the text file is arranged, row by row, in the same order as the index file.
The following code achieves what I'm looking to do, but I want to replace file_to_sort.txt with another variable (so that I can pass 2 command line arguments) to make this a general script. I can't figure out how to make the while loop deal with this.
while read line ; do grep $line file_to_sort.txt ; done < ../index.txt
The file to sort looks like so:
Locus ./PAK_01896.fsa GC: 0.401826484018
Locus ./PAK_02014.fsa GC: 0.355555555556
Locus ./PAK_02606.fsa GC: 0.415555555556
Locus ./PAK_03203.fsa GC: 0.391111111111
Locus ./PAU_01961.fsa GC: 0.395555555556
Locus ./PAU_02074.fsa GC: 0.406392694064
Locus ./PAU_02206.fsa GC: 0.353333333333
Locus ./PAU_02775.fsa GC: 0.415555555556
Locus ./PAU_03392.fsa GC: 0.384444444444
Locus ./PLT_01696.fsa GC: 0.42
Locus ./PLT_01716.fsa GC: 0.422222222222
Locus ./PLT_01736.fsa GC: 0.433333333333
Locus ./PLT_01758.fsa GC: 0.426666666667
Locus ./PLT_02424.fsa GC: 0.413333333333
Locus ./PLT_02568.fsa GC: 0.391111111111
And the index order looks like this (and so the output should be the first file, sorted according the the ./xxx_xxxx strings in the second).
PAU_03392
PAK_03203
PAU_01961
PAK_01787
PLT_02568
PAU_02074
PAK_01896
PLT_02424
PAU_02775
PLT_01696
PAK_02606
PLT_01736
PLT_01758
PLT_01716
PAU_02206
PAK_02014
I'm sure this is really simple, but I can't see it and whatever I was searching to try and solve this wasn't dragging up the right answers (since I'm sure this has been answered somewhere.
while..read..grepis anything but fast. You should use something likeawk -F'[/.]' 'NR==FNR{x[$3] = $0;next};($1 in x) {print x[$1]}' "$1" "$2"I'll let you figure out what the$1and respectively$2stand for (this is what your question is all about, after all).