在Linux中的每一列后面粘贴第一行

时间:2018-12-11 23:34:10

标签: linux bash awk

我有这样一个制表符分隔的文件(但具有更多的列)

1       2          3        4        5       6 
gene1   gene39   gene339   gene2     gene1   gene2
gene3   gene9    gene23    gene5     gene11  gene3
gene23  gene9    gene8     gene1     gene7   gene8

我要选择第1列,并将其粘贴到该列旁边。看起来像这样

gene1  1   gene39  2   gene339  3   gene2  4   gene1   5  gene2  6
gene3  1   gene9   2   gene23   3   gene5  4   gene11  5  gene3  6
gene23 1   gene9   2   gene8    3   gene1  4   gene7   5  gene8  6

任何帮助都会很棒!

4 个答案:

答案 0 :(得分:2)

$ awk 'BEGIN{FS=OFS="\t"} NR>1{ for (i=1; i<=NF; i++) printf "%s%s%s%s", $i, OFS, i, (i<NF ? OFS : ORS) }' file
gene1   1       gene39  2       gene339 3       gene2   4       gene1   5       gene2   6
gene3   1       gene9   2       gene23  3       gene5   4       gene11  5       gene3   6
gene23  1       gene9   2       gene8   3       gene1   4       gene7   5       gene8   6

答案 1 :(得分:0)

假设列按顺序编号,这行得通吗?

NR > 1 {
  for (i=1; i<=NF; i++) {
    printf "%s %d ", $i, i
  }
  print
}

答案 2 :(得分:0)

awk 'NR>1{for(i=1;i<NF;i++){ printf "%s\t%s\t", $i,i};printf "%s\t%s\n", $NF,NF}' steveman
gene1   1       gene39  2       gene339 3       gene2   4       gene1   5       gene2   6
gene3   1       gene9   2       gene23  3       gene5   4       gene11  5       gene3   6
gene23  1       gene9   2       gene8   3       gene1   4       gene7   5       gene8   6

答案 3 :(得分:0)

如果您正在考虑使用Perl,

$ cat steveman.txt
1       2          3        4        5       6
gene1   gene39   gene339   gene2     gene1   gene2
gene3   gene9    gene23    gene5     gene11  gene3
gene23  gene9    gene8     gene1     gene7   gene8
$ perl -F"/\s+/" -lane ' @FH=@F if $.==1; if($.>1) { for($i=0;$i<@F;$i++) { printf("%s\t%s\t",$F[$i],$FH[$i]) } print "" } ' steveman.txt
gene1   1       gene39  2       gene339 3       gene2   4       gene1   5       gene2   6
gene3   1       gene9   2       gene23  3       gene5   4       gene11  5       gene3   6
gene23  1       gene9   2       gene8   3       gene1   4       gene7   5       gene8   6
$