gpt4 book ai didi

linux - 根据第一列拆分第二列

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

我有两列是这样的:

cluster22717    GO:0005737,GO:0007049,GO:0051301

我怎样才能把它变成这样:

cluster22717    GO:0005737
cluster22717 GO:0007049
cluster22717 GO:0051301

我还应该提到,这是一个文件中的一行,该文件包含数千行这样的行,其中第二列具有不同数量的元素。提前致谢,佩兹曼·萨夫达里

最佳答案

最简单的解决方案是使用一些循环,请参见下面的示例,

输入文件:sample.txt

cluster22717    GO:0005737,GO:0007049,GO:0051301
cluster22717 GO:0005738,GO:0007041,GO:0051304,GO:0051307
cluster22717 GO:0005739,GO:0007042,GO:0051305,GO:0005737,GO:0007046
cluster22717 GO:0005740,GO:0007043,GO:0051306,GO:0005738,GO:0007041,GO:0051304

脚本:

while read line
do
var1=$(echo $line | awk '{print $1}') # assign first field to var1
Arrayvals=($(echo $line | awk '{print $2}' | sed -e 's/,/ /g')) # create an array from second filed

for (( i=0; i < ${#Arrayvals[@]} ; i++ )) # iterate the array using a for loop , ${#Arrayvals[@]} -> gives the length of array
do
echo "${var1} ${Arrayvals[${i}]}" # echo in desired format
done

done < sample.txt

输出:

cluster22717   GO:0005737
cluster22717 GO:0007049
cluster22717 GO:0051301
cluster22717 GO:0005738
cluster22717 GO:0007041
cluster22717 GO:0051304
cluster22717 GO:0051307
cluster22717 GO:0005739
cluster22717 GO:0007042
cluster22717 GO:0051305
cluster22717 GO:0005737
cluster22717 GO:0007046
cluster22717 GO:0005740
cluster22717 GO:0007043
cluster22717 GO:0051306
cluster22717 GO:0005738
cluster22717 GO:0007041
cluster22717 GO:0051304

希望这会有所帮助,

关于linux - 根据第一列拆分第二列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51173860/

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