linux中如何将一列数据转换为一行数据
1、生成测试数据
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt ## 测试数据 1 2 3 4 5
2、xargs实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | xargs 1 2 3 4 5
3、awk实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{printf("%s ", $0)} END {printf("\n")}' | cat -A 1 2 3 4 5 $ root@PC1:/home/test# cat a.txt | awk '{printf("%s ", $0)} END {printf("\n")}' | sed 's/.$//' | sed -n l 1 2 3 4 5$
4、paste实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | paste -s 1 2 3 4 5 root@PC1:/home/test# cat a.txt | paste -s -d " " 1 2 3 4 5 root@PC1:/home/test# cat a.txt | paste -s -d " " | cat -A 1 2 3 4 5$
5、tr实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | tr "\n" " " | sed 's/.$/\n/' 1 2 3 4 5 root@PC1:/home/test# cat a.txt | tr "\n" " " | sed 's/.$/\n/' | sed -n l 1 2 3 4 5$
6、利用内置变量RS实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# awk 'BEGIN{RS = EOF} {gsub("\n", " "); print}' a.txt 1 2 3 4 5 root@PC1:/home/test# awk 'BEGIN{RS = EOF} {gsub("\n", " "); print}' a.txt | cat -A 1 2 3 4 5$
7、利用RS、ORS内置变量实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{RS = "\n"; ORS = " "} {print}' 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{RS = "\n"; ORS = " "} {print} END {printf("\n")}' 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{RS = "\n"; ORS = " "} {print} END {printf("\n")}' 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{RS = "\n"; ORS = " "} {print} END {printf("\n")}' | cat -A 1 2 3 4 5 $ root@PC1:/home/test# cat a.txt | awk '{RS = "\n"; ORS = " "} {print} END {printf("\n")}' | sed 's/.$//' | cat -A 1 2 3 4 5$
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{ORS = " "} {print} END {printf("\n")}' 1 2 3 4 5 root@PC1:/home/test# cat a.txt | awk '{ORS = " "} {print} END {printf("\n")}' | cat -A 1 2 3 4 5 $ root@PC1:/home/test# cat a.txt | awk '{ORS = " "} {print} END {printf("\n")}' | sed 's/ $//' | cat -A 1 2 3 4 5$
8、sed实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 1 2 3 4 5 root@PC1:/home/test# cat a.txt | sed ':a; N; s/\n/ /; ta' 1 2 3 4 5 root@PC1:/home/test# cat a.txt | sed ':a; N; s/\n/ /; ta' | sed -n l 1 2 3 4 5$
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2021-03-22 c语言中数组中元素的个数