如何列的数据传送到行(与AWK)? [英] How to transfer the data of columns to rows (with awk)?
本文介绍了如何列的数据传送到行(与AWK)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的文件:
N A B C D
1 01 02 01 01
2 02 02 01 01
和我想通过行传递列,所以输出应该是这样的:
N + 1 2
A 01 02
乙02 02
Ç01 01
ð01 01
我已经写了这个命令:
的awk'{为(i = 1; I< = NF;我++)printf的$ I}'file.txt的>出file.txt的
问题是,这个命令把一切都在同一行!所以输出是这样的:
N + 1 2 A 01 02 B 02 02 01Çð01 01 01
解决方案
这可能工作:
的awk'{
关于(F = 1; F< = NF;˚F++){A [NR,F] = $ F}
}
NF> NF {NF = NF}
结束 {
关于(F = 1; F< = NF;˚F++){
为(R = 1;为r = NR; R ++){
printf的一个[R,F(R == NR RS:FS)
}
}
}'YOURINPUT
看到它在行动@ Ideone 。
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
解决方案
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.
这篇关于如何列的数据传送到行(与AWK)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文