linux - sed 字符串的第二次出现 - 对于外部文件中的所有行 (Linux)

标签 linux sed while-loop

我有一个文件,其第 2 列中的值需要重命名。在整个 ~5m 行文件 (with.duplicates) 中,有约 8k 个重复值(在文件 list.of.duplicates 中)。

数据集输入:

with.duplicates

1 rs143225517 0 751756 C T

1 rs146277091 0 752478 G

1 rs3094315 0 752566 G A

1 rs149886465 0 752617 A C

1 rs3131972 0 752721 G

1 rs3131972 0 752721 ATG

1 rs3131971 0 752894 T C

1 rs61770173 0 753405 C A

1 rs2073814 0 753474 CG

1 rs2073813 0 753541 G

1 rs12184325 0 754105 TC

list.of.duplicates

rs3131972

rs4310388

rs7529459

rs905135

rs9786995

rs12065710

rs6426404

rs12759849

rs6603823

我试过的代码

这正是我想要的 - 但效率低下且仅用于一次替换

sed -i '0,/rs3131972/! s/rs3131972/qrs3131972/' with.duplicates

但我不知道如何遍历整个重复值列表

i=0 
while ((i++)); 
read -r snp 
do 
sed -i '0,/${snp}/! s/${snp}/q${snp}/' with.duplicates 
done < list.of.duplicates

我在整个网站上找到了部分答案,但没有一个能将所有内容整合到一个有效的脚本中。

在此先感谢您的帮助!

在 Linux 或 R 中寻找解决方案

编辑:

期望的输出

1 rs143225517 0 751756 C T

1 rs146277091 0 752478 G

1 rs3094315 0 752566 G A

1 rs149886465 0 752617 A C

1 rs3131972 0 752721 G

1 qrs3131972 0 752721 ATG

1 rs3131971 0 752894 T C

1 rs61770173 0 753405 C A

1 rs2073814 0 753474 CG

1 rs2073813 0 753541 G

1 rs12184325 0 754105 TC

最佳答案

好吧,awk 可以自己处理这个问题。你不需要循环。

awk '(FNR==NR) { d[$1]; next }
     ($2 in d) && !(++d[$2]-2) { $2 = "q" $2; delete a[$2] }
     1' list.of.duplicates with.duplicates

Can it be modified to instead of adding the "q" to the second column of the second occurrence, to add the q to the second column of the longer line?

可以,但效率不如上述。

awk '(ARGIND==1) { d[$1]; next }
     (ARGIND==2) {
         if ($2 in d) {
             if ($2 in r) { if (length(r[$2]) > length()) d[$2]++; delete r[$2] }
             else { r[$2] = $0 }
         } next }
     ($2 in d) && !(++d[$2]-2) { $2 = "q" $2; delete d[$2] }
     1' list.of.duplicates with.duplicates with.duplicates

关于linux - sed 字符串的第二次出现 - 对于外部文件中的所有行 (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53840154/

相关文章:

c++ - ld 找不到库,即使它们存在

c - send(2) 接收 SIGPIPE 的目的是什么?

regex - sed 从正则表达式到正则表达式的打印行

java - 我用于确定适当字体大小的 while 循环太过分了......有时?

Java - 如何用 hasNext() 条件打破 while ?

c - Linux 内核从 kzalloc 取消引用 memset 中的 NULL 指针

linux - 全新的 Debian STS 安装在启动时崩溃

linux - 有没有办法将变量从 awk 语句作为参数传递给 bash 函数?

linux - 替换两个字符之间的逗号

python - 如果 while 循环中的语句不起作用