在一个文件中排序行给出的另一个文件中的顺序 [英] Sorting lines in one file given the order in another file
本文介绍了在一个文件中排序行给出的另一个文件中的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个文件1:
13 a b c d
5 f a c d
7 d c g a
14 a v s d
和一个文件2:
7 x
5 c
14 a
13 i
我想进行排序的file1考虑file2中的第一列的相同的顺序,以使输出应该是:
I would like to sort file1 considering the same order of the first column in file2, so that the output should be:
7 d c g a
5 f a c d
14 a v s d
13 a b c d
是否有可能做到这一点在bash或者我应该使用一些高级的语言如Python?
Is it possible to do this in bash or should I use some "higher" language like python?
推荐答案
使用 AWK
放线数量从文件2
在文件1
一个额外的列。按该列的结果。然后删除preFIX列
Use awk
to put the line number from file2
as an extra column in front of file1
. Sort the result by that column. Then remove that prefix column
awk 'FNR == NR { lineno[$1] = NR; next}
{print lineno[$1], $0;}' file2 file1 | sort -k 1,1n | cut -d' ' -f2-
这篇关于在一个文件中排序行给出的另一个文件中的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文