001、 方法1
[root@pc1 test01]# ls a.txt test.sh [root@pc1 test01]# cat a.txt ## 测试数据, 依据最后一列展开为多行 chrY 2657879 2658063 CTCF 652 GM19239 chrY 2664424 2664734 CTCF 185 hL-hESC,HepG2,HUVEC chrY 2668277 2668694 CTCF 766 Dnd41,H1-hESC,HepG2,HSMM,HSMMtube,HUVEC,NH-A,A549,ProgFib,AG10803,BJ,GM12864,HCM,HCPEpic,HEEpiC,HFF,HEF-Myc,HPF,HVMF,NHLF,RPTEC,SAEC [root@pc1 test01]# cat test.sh ## 展开程序 #!/bin/bash for i in $(seq $(wc -l < a.txt)) do sed -n "$i"p a.txt | awk '{OFS = "\t"; split($NF, ay, ","); len = length(ay); for (i = 1; i <= len; i++) {print $0, ay[i]}}' | awk '{OFS = "\t"; $(NF - 1) = $NF; sub(/\s*\S*$/, ""); print $0}' >> result.txt done [root@pc1 test01]# bash test.sh ## 执行程序 [root@pc1 test01]# ls a.txt result.txt test.sh [root@pc1 test01]# head result.txt -n 6 ## 运行结果 chrY 2657879 2658063 CTCF 652 GM19239 chrY 2664424 2664734 CTCF 185 hL-hESC chrY 2664424 2664734 CTCF 185 HepG2 chrY 2664424 2664734 CTCF 185 HUVEC chrY 2668277 2668694 CTCF 766 Dnd41 chrY 2668277 2668694 CTCF 766 H1-hESC
002、方法2
标签:多行,test01,某列,pc1,chrY,linux,txt,root,CTCF From: https://www.cnblogs.com/liujiaxin2018/p/17697978.html