linux 系统中把一列数据变为一行数据

1、测试数据

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10

 

2、xargs

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
[root@centos79 test]# cat a.txt | xargs
01 02 03 04 05 06 07 08 09 10

 

3、tr命令

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
[root@centos79 test]# cat a.txt | tr "\n" " " | sed 's/.$/\n/'
01 02 03 04 05 06 07 08 09 10

 

4、awk命令

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
[root@centos79 test]# awk '{ORS = " "}{print} END {printf "\n"}' a.txt
01 02 03 04 05 06 07 08 09 10

 

5、awk命令

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
[root@centos79 test]# awk '{printf("%s ", $0)} END {printf "\n"}' a.txt
01 02 03 04 05 06 07 08 09 10

 

6、sed

[root@centos79 test]# cat a.txt
01
02
03
04
05
06
07
08
09
10
[root@centos79 test]# sed ':a; N; s/\n/ /; ta' a.txt
01 02 03 04 05 06 07 08 09 10

 

验证:

[root@centos79 test]# cat a.txt
s e t
g i k
z n c
d w u
[root@centos79 test]# cat a.txt | xargs
s e t g i k z n c d w u
[root@centos79 test]# cat a.txt | tr "\n" " " | sed 's/.$/\n/'
s e t g i k z n c d w u
[root@centos79 test]# awk '{ORS = " "} {print} END {printf "\n"}' a.txt
s e t g i k z n c d w u
[root@centos79 test]# awk '{printf("%s ", $0)} END {printf "\n"}' a.txt
s e t g i k z n c d w u
[root@centos79 test]# sed ':a; N; s/\n/ /; ta' a.txt
s e t g i k z n c d w u

 

posted @ 2021-07-22 00:18  小鲨鱼2018  阅读(306)  评论(0编辑  收藏  举报