1、测试数据
root@PC1:/home/test# ls outcome.ped root@PC1:/home/test# cat outcome.ped ## 测试数据, 将每隔一个空格的两列并排为一列 G G C C G G G G G A A A G G G C G G G C G G G G G G A A A G C C G G C C G G G G G G A A A G G C G G C C G G G G G G A A G G G G G G C C G G G G G G A A A G G C G G C C G G G G G G A A A A C C G G C C G G A G A A A A G G C C G G C C G G A G A A A A G G C C root@PC1:/home/test# cp outcome.ped outcome.ped_bak ## 备份数据 root@PC1:/home/test# ls outcome.ped outcome.ped_bak
2、sed实现
root@PC1:/home/test# ls outcome.ped outcome.ped_bak root@PC1:/home/test# cat outcome.ped ## 测试数据 G G C C G G G G G A A A G G G C G G G C G G G G G G A A A G C C G G C C G G G G G G A A A G G C G G C C G G G G G G A A G G G G G G C C G G G G G G A A A G G C G G C C G G G G G G A A A A C C G G C C G G A G A A A A G G C C G G C C G G A G A A A A G G C C root@PC1:/home/test# let num=$(head -n 1 outcome.ped | awk '{print NF}')/2 ## 统计列数并确定需要替换的列数 root@PC1:/home/test# echo $num 8 root@PC1:/home/test# for i in `seq $num`; do sed "s/ //$i" outcome.ped -i; done ## 利用sed替换 root@PC1:/home/test# ls outcome.ped outcome.ped_bak root@PC1:/home/test# cat outcome.ped ## 查看替换效果 GG CC GG GG GA AA GG GC GG GC GG GG GG AA AG CC GG CC GG GG GG AA AG GC GG CC GG GG GG AA GG GG GG CC GG GG GG AA AG GC GG CC GG GG GG AA AA CC GG CC GG AG AA AA GG CC GG CC GG AG AA AA GG CC
3、awk 实现
root@PC1:/home/test# ls outcome.ped root@PC1:/home/test# cat outcome.ped G G C C G G G G G A A A G G G C G G G C G G G G G G A A A G C C G G C C G G G G G G A A A G G C G G C C G G G G G G A A G G G G G G C C G G G G G G A A A G G C G G C C G G G G G G A A A A C C G G C C G G A G A A A A G G C C G G C C G G A G A A A A G G C C root@PC1:/home/test# cp outcome.ped outcome.ped_bak ## 备份数据 root@PC1:/home/test# ls outcome.ped outcome.ped_bak root@PC1:/home/test# awk '{for(i = 1; i <= NF; i++) if(i % 2 != 0) {printf("%s", $i)} else {printf("%s ", $i)}; printf("\n")}' outcome.ped ## 利用awk列循环判断实现 GG CC GG GG GA AA GG GC GG GC GG GG GG AA AG CC GG CC GG GG GG AA AG GC GG CC GG GG GG AA GG GG GG CC GG GG GG AA AG GC GG CC GG GG GG AA AA CC GG CC GG AG AA AA GG CC GG CC GG AG AA AA GG CC