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 @   小鲨鱼2018  阅读(151)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-06-22 R语言中实现在命令行中传参
2022-06-22 R语言中read.table中colClasses = "character"选项作用
2021-06-22 c语言中以10进制、8进制、16进制显示同一个数字
点击右上角即可分享
微信分享提示