首页 > 系统相关 >linux 中如何将一列数据转换为指定列的数据

linux 中如何将一列数据转换为指定列的数据

时间:2022-10-29 23:56:22浏览次数:45  
标签:转换 数据 pc1 cat 一列 linux test txt root

 

 

001、

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt      ## 测试数据
1
2
3
4
5
6
7
8
9
10               ## 转换为两列数据
[root@pc1 test]# cat a.txt | paste -s -d " " | awk '{for (i = 1; i <= NF; i++) {if(i % 2 == 0) {print $i} else {printf("%s ", $i)}}; if((i - 1) % 2 != 0) {printf("\n")}}'
1 2
3 4
5 6
7 8
9 10

 

 

002、转换为3列数据

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
1
2
3
4
5
6
7
8
9
10              ## 转换为3列数据
[root@pc1 test]# cat a.txt | paste -s -d " " | awk '{for (i = 1; i <= NF; i++) {if(i % 3 == 0) {print $i} else {printf("%s ", $i)}}; if((i - 1) % 3 != 0) {printf("\n")}}'
1 2 3
4 5 6
7 8 9
10

 

 

003、转换为7列数据

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
1
2
3
4
5
6
7
8
9
10
[root@pc1 test]# cat a.txt | paste -s -d " " | awk '{for (i = 1; i <= NF; i++) {if(i % 7 == 0) {print $i} else {printf("%s ", $i)}}; if((i - 1) % 7 != 0) {printf("\n")}}'
1 2 3 4 5 6 7
8 9 10

 

标签:转换,数据,pc1,cat,一列,linux,test,txt,root
From: https://www.cnblogs.com/liujiaxin2018/p/16840258.html

相关文章