Linux 如何将列的数据传输到行(使用awk)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9534744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:57:22  来源:igfitidea点击:

How to transfer the data of columns to rows (with awk)?

linuxawkrows

提问by mahmood

I have a file like this:

我有一个这样的文件:

n A B C D 
1 01 02 01 01
2 02 02 01 01

and I want to transfer the columns by rows, so the output should be like this:

我想按行传输列,所以输出应该是这样的:

n 1 2
A 01 02
B 02 02
C 01 01 
D 01 01

I have wrote this command:

我写了这个命令:

awk '{ for (i=1;i<=NF;i++ ) printf $i " " }' file.txt > out-file.txt

the problem is that this command put everything on one line! so the output is like this:

问题是这个命令把所有东西都放在一行上!所以输出是这样的:

n 1 2 A 01 02 B 02 02 C 01 01 D 01 01

采纳答案by Zsolt Botykai

This might work:

这可能有效:

awk '{
       for (f = 1; f <= NF; f++) { a[NR, f] = $f } 
     }
     NF > nf { nf = NF }
     END {
       for (f = 1; f <= nf; f++) {
           for (r = 1; r <= NR; r++) {
               printf a[r, f] (r==NR ? RS : FS)
           }
       }
    }' YOURINPUT

See it in action @ Ideone.

看看它在行动@ Ideone

回答by Eduardo Ivanec

Save this script as transpose.awk and chmod u+x transpose.awk. It's a modification of Tim Sherwood'stranspose.

将此脚本另存为 transpose.awk 和chmod u+x transpose.awk. 这是蒂姆·舍伍德( Tim Sherwood) 的修改版transpose

#!/usr/bin/gawk -f

BEGIN {
    max_x =0;
    max_y =0;
}

{
    max_y++;
    for( i=1; i<=NF; i++ )
    {
        if (i>max_x) max_x=i;
        A[i,max_y] = $i;
    }
}

END {
    for ( x=1; x<=max_x; x++ )
    {
        for ( y=1; y<=max_y; y++ )
        {
            if ( (x,y) in A ) printf "%s",A[x,y];
            if ( y!=max_y ) printf " ";
        }
        printf "\n";
    }
}

Example:

例子:

$ ./transpose.awk example
n 1 2
A 01 02
B 02 02
C 01 01
D 01 01

回答by Hai Vu

Here is a different solution, which involve only one for loop at the end:

这是一种不同的解决方案,它最后只涉及一个 for 循环:

{ for (i=1; i<=NF; i++) col[i] = col[i] " " $i }
END { 
    for (i=1; i<=NF; i++) { 
        sub(/^ /, "", col[i]); 
        print col[i] 
    } 
}

Discussion

讨论

  • This solution uses a single-dimension array col, which stores the value for the whole column. col[1]is the first column.
  • For every line, we append the column to col[i]. Because we blindly append, the col[i]value will contain a leading space.
  • At the end, the sub() function removes the leading white space before printing out the column (now a row)
  • 此解决方案使用单维数组col,该数组存储整列的值。col[1]是第一列。
  • 对于每一行,我们将列附加到 col[i]。因为我们盲目追加,所以col[i]值将包含一个前导空格。
  • 最后, sub() 函数在打印出列(现在是一行)之前删除前导空格

回答by chavoosh

Well, without awk we can do this with cat:

好吧,没有 awk 我们可以这样做cat

for x in `cat filename`
do
echo $x
done

With this script the result will emerge in cols.

使用此脚本,结果将出现在 cols 中。

回答by nisetama

It would be easier to use rs:

使用起来会更容易rs

$ cat /tmp/a
n A B C D
1 01 02 01 01
2 02 02 01 01
$ rs -c' ' -C' ' -T</tmp/a
n 1 2
A 01 02
B 02 02
C 01 01
D 01 01

-cchanges the input column separator, -Cchanges the output column separator, and -Ttransposes rows and columns.

-c更改输入列分隔符,-C更改输出列分隔符,并-T转置行和列。