我想根据行中的前几个字符从文件中随机选择行。在我的文件中,前几个字符被命名为“杂交: 2",”混合: 19“等等。我想从每个混合分类中随机选择一个有代表性的行。
我有一个文件格式如下:
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会有用,但我看不出如何在这里应用它。我不知道该如何处理,所以任何帮助都将不胜感激!
发布于 2017-04-06 09:17:18
得到独特的线条(杂交19,杂交52,杂交44,杂交50,杂交2)。在此之后对文件进行洗牌。洗牌将返回文件中行的随机排列。获取洗牌文件的第一行。
这样做的代码如下
将下面的脚本复制到一个文件中,比如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.txthttps://stackoverflow.com/questions/43214002
复制相似问题