我正在尝试从 bash 中的文件名列表中动态提取独特的模式。
输入的文件名列表如下所示
Exp1_ML_Rep1.txt,Exp1_ML_Rep2.txt,Exp1_ML_Rep3.txt
我想动态提取字符串
Rep1,Rep2,Rep3
此处以图表方式显示:
注意:输入模式每次都可能改变 例如另一个用例可能是
Exp2_DT_10ng_55C_1_User1.png,Exp2_DT_10ng_55C_2_User1.png,Exp2_DT_10ng_55C_3_User1.png
在这种情况下,我想提取
1,2,3
此处以图表方式显示:
在 bash 中实现此目的的最佳方法是什么?
按照评论中的建议,我尝试了以下方法:
declare -p string1 string2
declare -- string1="ER_Rep1"
declare -- string2="ER_Rep2"
diff <(echo "$string1" ) <(echo "$string2")
返回
1c1
< ER_Rep1
---
> ER_Rep2
我要提取的是 Rep1,Rep2。
最佳答案
您可以将 GNU awk
与 sort
和 uniq
结合使用
echo 'Exp1_ML_Rep1.txt,Exp1_ML_Rep2.txt,Exp1_ML_Rep3.txt' | awk -v RS='[_.,]' '1' | sort | uniq -u
或
tr
结合 sort
& uniq
echo 'Exp1_ML_Rep1.txt,Exp1_ML_Rep2.txt,Exp1_ML_Rep3.txt' | tr '_.,' '\n' | sort | uniq -u
产生输出
Rep1
Rep2
Rep3
关于bash - 动态提取 bash 中字符串列表中每个字符串唯一的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68274814/