gpt4 book ai didi

linux - 如何合并特定行中两列的内容而不移动列的其余单元格

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:18 24 4
gpt4 key购买 nike

我有制表符分隔的输入文件,其中第二列的一些内容是空格分隔的,因此在两列之间划分,以空格作为分隔符,例如“LEA类型”完全属于相同的第二列,但其划分方式是“LEA”出现在第二列中,“类型”出现在第三列中,类似地“核糖体蛋白L21P”是相同的名称应该出现在第二列下,但分为第二列,第三列和第四列。

1st_col     2nd_col     3rd_col    4th_col  5th_col 6th_col
tATAAAta TBP ~ 1
tACCAT Ribosomal protein L21P ~ 2
agtACCAT Ribosomal protein L21P ~ 2
ATGTActt AP2 ~ 1
GCAACggagc LEA type 1 ~ 1
ATGGTa Ribosomal protein L21P ~ 1
ATGGTctt Ribosomal protein L21P ~ 2
ATGGTaca Ribosomal protein L21P ~ 1

期望的输出应该是这样的,这样“LEA type”应该像这样“LEA_type”一样位于第二列,并且其他单元格的位置和内容不会移动。

1st_col     2nd_col                 3rd_col 4th_col 5th_col 6th_col
tATAAAta TBP ~ 1
tACCAT Ribosomal_protein_L21P ~ 2
agtACCAT Ribosomal_protein_L21P ~ 2
ATGTActt AP2 ~ 1
GCAACggagc LEA_type ~ 1
ATGGTa Ribosomal_protein_L21P ~ 1
ATGGTctt Ribosomal_protein_L21P ~ 2
ATGGTaca Ribosomal_protein_L21P ~ 1

我已经尝试过类似的方法,但它也会导致其他细胞发生变化。

 sed 's/LEA\stype/LEA_type/g' 1_com_final_2922.txt | sed 's/Ribosomal\sprotein/Ribosomal_protein/g'

提前致谢。

最佳答案

这是更灵活的方法,

awk '$2~/[^0-9|^~]+/{                  # search the line which $2 is not numeric nor tide
for(i=3;i<=NF;i++){ # continue to search start from $3
if($i~/[^0-9|^~]+/){ # if $i is not numeric nor tide
$2=sprintf("%s_%s",$2,$i); # substitute $2 as $2_$i
$i="" # set $i=""
} else # if hit something numeric or tide, we break
break
}
}1'

这是一句,

awk '$2~/[^0-9|^~]+/{for(i=3;i<=NF;i++){ if($i~/[^0-9|^~]+/){ $2=sprintf("%s_%s",$2,$i); $i="" } else break } }1' file

编辑:

更新了更新OP的答案,

awk '$3~/[^~]/ && NR>1{for(i=3;i<=NF;i++){ if($i~/[^0-9|^~]+/){ $2=sprintf("%s_%s",$2,$i); $i="" } else{$3="~"; $4=$(i+1); $i=""; $(i+1)=""; break} } }1' file5 | column -t

关于linux - 如何合并特定行中两列的内容而不移动列的其余单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com