Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash-根据另一个文件中的列表对文件进行排序_Bash_Sorting - Fatal编程技术网

Bash-根据另一个文件中的列表对文件进行排序

Bash-根据另一个文件中的列表对文件进行排序,bash,sorting,Bash,Sorting,我知道有人问过类似的问题,关于按特定列对文件进行排序,但他们似乎都没有回答我的问题 我的输入文件看起来像 OHJ07_1_contig_10 0 500 130 500 500 1.0000000 OHJ07_1_contig_10 500 1000 180 500 500 1.0000000 OHJ07_1_contig_10 1000 1500 171 500 500 1.0000000 OHJ07_1_contig_10 1500 2000

我知道有人问过类似的问题,关于按特定列对文件进行排序,但他们似乎都没有回答我的问题

我的输入文件看起来像

OHJ07_1_contig_10   0   500 130 500 500 1.0000000
OHJ07_1_contig_10   500 1000    180 500 500 1.0000000
OHJ07_1_contig_10   1000    1500    171 500 500 1.0000000
OHJ07_1_contig_10   1500    2000    79  380 500 0.7600000
OHJ07_1_contig_10   2000    2500    62  500 500 1.0000000
OHJ07_1_contig_10   2500    3000    96  500 500 1.0000000
OHJ07_1_contig_10   3000    3500    76  500 500 1.0000000
OHJ07_1_contig_10   3500    4000    87  500 500 1.0000000
OHJ07_1_contig_10   4000    4500    60  500 500 1.0000000
OHJ07_1_contig_10   4500    5000    64  500 500 1.0000000
OHJ07_1_contig_10   5000    5468    213 468 468 1.0000000
OHJ07_1_contig_100  0   500 459 500 500 1.0000000
OHJ07_1_contig_100  500 1000    156 500 500 1.0000000
OHJ07_1_contig_100  1000    1314    77  305 314 0.9713376
OHJ07_1_contig_1000 0   500 239 500 500 1.0000000
OHJ07_1_contig_1000 500 1000    226 500 500 1.0000000
OHJ07_1_contig_1000 1000    1500    238 500 500 1.0000000
OHJ07_1_contig_1000 1500    2000    263 500 500 1.0000000
生成它的程序根据第一列中的名称按字母数字排序,但我想根据另一个文件中的名称列表对其排序,并保留所有其他数据。另一个文件具有其他信息,如第2列中的重叠长度(此文件是使用samtools faidx生成的)

由于每个名称在第一个文件中有不同数量的条目,那么最简单的处理方法是什么?最好使用bash


我没有尝试过任何东西,因为我根本没有办法解决这个问题。

我会在决定顺序(从现在起命名索引)的每一行文件前面加上行号,有一种方法使用awk,我使用这里写的答案来做这件事(假设您的索引文件名为index,数据文件名为data.txt):

通过这种方式,您将能够使用第一个字段(数字)进行排序,该字段将您的任意顺序转换为数字顺序

创建与上述编号相同的新数据文件的while:

while read line
do 
   key=$(echo $line | cut -f1)
   n=$(grep $key index-numbered | cut -d, -f1)
   echo $n","$line >> indexed-data.txt
done < data.txt
如果要隐藏最终输出上的行号,可以使用以下命令过滤掉每个行号:

sort -k1 -n -t, indexed-data.txt | cut -d, -f2 > sorted-data.txt
最终输出将在sorted-data.txt文件中


我相信这不是最好的解决方案,也许其他人的回答比我好。

请分享您迄今为止的尝试。我想根据另一个文件中的名称列表对其进行排序这到底意味着什么?使用该顺序并放弃其他文件中未提及的所有其他文件?请显示您的问题。你应该包括一个你有问题的代码,然后我们可以尝试帮助解决具体的问题。你也应该阅读。你有多少行或记录?另外,发布这样的数据,使其与另一个文件中的名称列表相匹配。
57,OHJ07_1_contig_46339    411691  143341916   60  61
while read line
do 
   key=$(echo $line | cut -f1)
   n=$(grep $key index-numbered | cut -d, -f1)
   echo $n","$line >> indexed-data.txt
done < data.txt
sort -k1 -n -t, indexed-data.txt >sorted-data.txt
sort -k1 -n -t, indexed-data.txt | cut -d, -f2 > sorted-data.txt