bash - Sorting lines in one file given the order in another file -


given file1:

13 b c d 5 f c d 7 d c g 14 v s d 

and file2:

7 x 5 c 14 13 

i sort file1 considering same order of first column in file2, output should be:

7 d c g 5 f c d 14 v s d 13 b c d 

is possible in bash or should use "higher" language python?

use awk put line number file2 column in front of file1. sort result column. remove prefix column

awk 'fnr == nr { lineno[$1] = nr; next}      {print lineno[$1], $0;}' file2 file1 | sort -k 1,1n | cut -d' ' -f2- 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -