在Unix中,如何根据字符从文件中随机选择行?
我想根据行中的前几个字符从文件中随机选择行。在我的文件中,前几个字符被命名为“Hybrid:2”、“Hybrid:19”等。我想从每个Hybrid类别中随机选择一个代表行 我有一个格式如下的文件:在Unix中,如何根据字符从文件中随机选择行?,unix,Unix,我想根据行中的前几个字符从文件中随机选择行。在我的文件中,前几个字符被命名为“Hybrid:2”、“Hybrid:19”等。我想从每个Hybrid类别中随机选择一个代表行 我有一个格式如下的文件: Hybrid: 2 Parents: 12 and 49 9.419642 0.000000 Hybrid: 2 Parents: 15 and 49 6.427708 0.000000 Hybrid: 2 Parents: 16 and 49 8.383469 0.00000
Hybrid: 2 Parents: 12 and 49 9.419642 0.000000
Hybrid: 2 Parents: 15 and 49 6.427708 0.000000
Hybrid: 2 Parents: 16 and 49 8.383469 0.000000
Hybrid: 2 Parents: 17 and 49 7.869732 0.000000
Hybrid: 19 Parents: 2 and 23 6.905067 0.000000
Hybrid: 19 Parents: 2 and 30 7.913185 0.000000
Hybrid: 19 Parents: 2 and 57 10.724088 0.000000
Hybrid: 52 Parents: 2 and 23 9.398697 0.000000
Hybrid: 44 Parents: 2 and 30 7.739156 0.000000
Hybrid: 50 Parents: 2 and 30 10.051190 0.000000
Hybrid: 2 Parents: 15 and 49 6.427708 0.000000
Hybrid: 19 Parents: 2 and 23 6.905067 0.000000
Hybrid: 52 Parents: 2 and 23 9.398697 0.000000
Hybrid: 44 Parents: 2 and 30 7.739156 0.000000
Hybrid: 50 Parents: 2 and 30 10.051190 0.000000
我想制作这样的文件:
Hybrid: 2 Parents: 12 and 49 9.419642 0.000000
Hybrid: 2 Parents: 15 and 49 6.427708 0.000000
Hybrid: 2 Parents: 16 and 49 8.383469 0.000000
Hybrid: 2 Parents: 17 and 49 7.869732 0.000000
Hybrid: 19 Parents: 2 and 23 6.905067 0.000000
Hybrid: 19 Parents: 2 and 30 7.913185 0.000000
Hybrid: 19 Parents: 2 and 57 10.724088 0.000000
Hybrid: 52 Parents: 2 and 23 9.398697 0.000000
Hybrid: 44 Parents: 2 and 30 7.739156 0.000000
Hybrid: 50 Parents: 2 and 30 10.051190 0.000000
Hybrid: 2 Parents: 15 and 49 6.427708 0.000000
Hybrid: 19 Parents: 2 and 23 6.905067 0.000000
Hybrid: 52 Parents: 2 and 23 9.398697 0.000000
Hybrid: 44 Parents: 2 and 30 7.739156 0.000000
Hybrid: 50 Parents: 2 and 30 10.051190 0.000000
我认为shuf会有用,但我不知道如何在这里应用它。我不知道如何处理这个问题,所以任何帮助都将不胜感激 获得独特的品系(杂交19、杂交52、杂交44、杂交50、杂交2)。在此之后,洗牌文件。Shuffle将返回文件中行的随机排列。获取洗牌文件的第一行
下面是执行此操作的代码
将下面的脚本复制到一个文件,例如UniqueLines.sh
#!/bin/bash
filePath=$1
cat $filePath | awk '{print $2}' | uniq > /tmp/unique_ids
for i in `cat /tmp/unique_ids`;do shuf file.txt | grep "Hybrid: $i " | head -1;done;
授予运行脚本的权限
chmod +x UniqueLines.sh
./UniqueLines.sh <full path to file>
Example:
./UniqueLines.sh /root/myfile.txt
运行脚本
chmod +x UniqueLines.sh
./UniqueLines.sh <full path to file>
Example:
./UniqueLines.sh /root/myfile.txt
/UniqueLines.sh
例子:
./UniqueLines.sh/root/myfile.txt