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).
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.