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


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

 

posted @ 2023-06-22 22:18  小鲨鱼2018  阅读(138)  评论(0编辑  收藏  举报