Reputation: 13
I would need to sort space separated text file which looks something like this:
rs4771122 1.983 0.998 1.998 1.990 0.998 0.988 1.000 1.984 0.001 1.998 1.996
rs1333026 0.000 0.000 0.000 1.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000
rs11847697 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000
rs10150332 1.000 0.000 1.000 0.000 1.000 0.000 1.000 1.000 0.975 0.000 1.000
etc.
It should be sorted by first column according to the order listed in separate text file containing all the values in first column. Second file looks like this:
rs1333026
rs11847697
rs4771122
rs10150332
etc.
And output should look like this:
rs1333026 0.000 0.000 0.000 1.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000
rs11847697 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000
rs4771122 1.983 0.998 1.998 1.990 0.998 0.988 1.000 1.984 0.001 1.998 1.996
rs10150332 1.000 0.000 1.000 0.000 1.000 0.000 1.000 1.000 0.975 0.000 1.000
etc.
Is there a way to use sort command similarly to grep with -f flag in order to obtain this?
Thank you.
Upvotes: 0
Views: 84
Reputation: 289625
You can loop through the second file and keep grep
ing each line in the first file.
Like this, for example:
while read line
do
grep -wF "$line" f1
done < f2
grep -w
matches full words.grep -F
matches fixed strings, so no regex.$ while read line; do grep "$line" f1; done < f2
rs1333026 0.000 0.000 0.000 1.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000
rs11847697 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000
rs4771122 1.983 0.998 1.998 1.990 0.998 0.988 1.000 1.984 0.001 1.998 1.996
rs10150332 1.000 0.000 1.000 0.000 1.000 0.000 1.000 1.000 0.975 0.000 1.000
Upvotes: 3