linux 中 将所有的数据转换为一行

发布时间 2023-06-22 22:23:13作者: 小鲨鱼2018


001、

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt             ## 测试数据
1 2 3
4 5 6
7 8
[root@PC1 test01]# cat a.txt | paste -s -d " "     ## 转换为一行
1 2 3 4 5 6 7 8

 

002、awk实现

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt
1 2 3
4 5 6
7 8
[root@PC1 test01]# awk '{printf("%s ", $0)} END {printf("\n")}' a.txt
1 2 3 4 5 6 7 8

 

003、tr实现

a、

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt
1 2 3
4 5 6
7 8
[root@PC1 test01]# cat a.txt | tr "\n" " " | xargs echo
1 2 3 4 5 6 7 8

 

b、

[root@PC1 test01]# ls
a.txt
[root@PC1 test01]# cat a.txt
1 2 3
4 5 6
7 8
[root@PC1 test01]# cat a.txt | tr "\012" " " | sed 's/$/\n/'
1 2 3 4 5 6 7 8