bash - 在非标签行上重复上一个标签并增加计数
问题描述
我有以下包含基因名称和外显子位置的字符串。
gene1
250405..250551
251490..251884
195620..195641
154254..155469
156319..156548
gene2
171224..171481
403914..403970
147436..147627
149077..149157
11635..12021
gene3
107657..107844
105642..106214
103531..103691
407044..407435
405691..405843
我想编写一个脚本,列出所有基因名称和输出位置应该如下所示:
gene1_exon1 250405..250551
gene1_exon2 251490..251884
geneN_exonN X..Y
任何想法我该怎么做?
我尝试了以下脚本
grep [a-z] temp3 > names
cat names |
while read line; do
cat temp3 |
while read position; do
if [ "$line" != "$position" ] ; then
echo "$line" ${position} >> names_and_positions
else
break
fi
done
done
解决方案
拉出基因名称并在每个外显子线之前重复。
awk '!/^[0-9]+\.\.[0-9]+$/ { gene=$1; i=1; next }
{ print gene "_exon" i++, $0 }' input >output
在看起来不像外显子行的行上,捕获第一个标记作为基因名称,并从 1 开始计算外显子行。在其他行(即外显子行)上打印带有捕获字符串和外显子行号前置的行,并增加数字。
这显然取决于正确使用正则表达式。如果您的外显子信息可能包含其周围的空格或其他变体,则您需要相应地调整正则表达式。
推荐阅读
- xml - xml 文件加载显示 json 错误
- php - Codeigniter 在 Mac 上使用不同的 MYSQL
- python - 如何从允许重复的python列表中获取所有可能的组合
- docker - Docker AddSigningCredential BIO_new_file:没有这样的文件
- regex - 使用 sed 命令将一个数字替换为另一个数字的正则表达式失败
- php - 如何在 wordpress 中使用 function_exists() 或 class_exists() 而不是 is_plugin_active()
- mysql - 如何使用存储在 web.config 中的连接字符串在 ASP .NET Razor MVC 中制定 MySQL 连接查询
- apache-zeppelin - 无法为 zeppelin 进行 SSO 连接
- javascript - 使用 CSV 数据构建 XML
- javascript - 如何从 weppalyzer 隐藏 javascript、web 框架版本详细信息