linux系统中将一列数据转换为若干列数据(列的顺序不变)

1、测试数据

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20

2、生成中间文件,假设4行为一列

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
[root@centos79 test]# awk '{if(NR % 4 == 0) {print $0} else {printf "%s ", $0}}' a.txt | tee tmp
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
17 18 19 20
[root@centos79 test]# cat tmp
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
17 18 19 20

3、对临时文件进行转置

[root@centos79 test]# cat tmp
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
17 18 19 20
[root@centos79 test]# for i in $(seq $(head -n 1 tmp | awk '{print NF}')); do cut -d " " -f $i tmp | awk '{ORS = " "}{print $0} END {printf "
"}' >> b.txt; done
[root@centos79 test]# cat b.txt
01 05 09 13 17
02 06 10 14 18
03 07 11 15 19
04 08 12 16 20
[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15059522.html