linux系统中如何将多列数据转化为一列数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux系统中如何将多列数据转化为一列数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5989字,纯文字阅读大概需要9分钟。
内容图文
![linux系统中如何将多列数据转化为一列数据](/upload/InfoBanner/zyjiaocheng/936/6d91c43d30fe4fc28c6179e2f338321b.jpg)
1、利用xargs命令将多列数据转化为一列数据
[root@linuxprobe test2]# echo "1 2 3 4 5 6" > a.txt [root@linuxprobe test2]# cat a.txt 1 2 3 4 5 6 [root@linuxprobe test2]# cat a.txt | xargs -n 1 ## 直接加参数 -n 1 即可 1 2 3 4 5 6
[root@linuxprobe test2]# seq 10000 | tr "\n" " " | sed 's/$/\n/' > b.txt ## 测试单行多列 [root@linuxprobe test2]# wc -l b.txt ## 1行 1 b.txt [root@linuxprobe test2]# awk '{print NF}' b.txt ##10000列 10000 [root@linuxprobe test2]# time xargs -n 1 < b.txt | wc -l 10000 real 0m5.752s user 0m2.685s sys 0m3.608s [root@linuxprobe test2]# seq 50000 | tr "\n" " " | sed 's/$/\n/' > b.txt [root@linuxprobe test2]# wc -l b.txt 1 b.txt [root@linuxprobe test2]# awk '{print NF}' b.txt 50000 [root@linuxprobe test2]# time xargs -n 1 < b.txt | wc -l 50000 real 0m28.813s user 0m13.534s sys 0m18.066s [root@linuxprobe test2]# seq 100000 | tr "\n" " " | sed 's/$/\n/' > b.txt [root@linuxprobe test2]# wc -l b.txt 1 b.txt [root@linuxprobe test2]# awk '{print NF}' b.txt 100000 [root@linuxprobe test2]# time xargs -n 1 < b.txt | wc -l ## 比较耗时 100000 real 0m56.833s user 0m26.998s sys 0m35.685s
[root@linuxprobe test2]# seq 10000 | tr "\n" " " | sed 's/$/\n/' > b.txt ## 测试数据多行多列的情况 [root@linuxprobe test2]# wc -l b.txt 1 b.txt [root@linuxprobe test2]# awk '{print NF}' b.txt 10000 [root@linuxprobe test2]# cat b.txt b.txt > c.txt [root@linuxprobe test2]# wc -l c.txt 2 c.txt [root@linuxprobe test2]# awk '{print NF}' c.txt 10000 10000 [root@linuxprobe test2]# time xargs -n 1 < c.txt | wc -l ## 耗时随行数增加,近似翻倍 20000 real 0m11.407s user 0m5.317s sys 0m7.290s [root@linuxprobe test2]# seq 100000 | tr "\n" " " | sed 's/$/\n/' > b.txt [root@linuxprobe test2]# wc -l b.txt 1 b.txt [root@linuxprobe test2]# awk '{print NF}' b.txt 100000 [root@linuxprobe test2]# cat b.txt b.txt > c.txt [root@linuxprobe test2]# wc -l c.txt 2 c.txt [root@linuxprobe test2]# awk '{print NF}' c.txt 100000 100000 [root@linuxprobe test2]# time xargs -n 1 < c.txt | wc -l ## 比较耗时 200000 real 1m54.300s user 0m53.625s sys 1m12.667s
2、利用sed命令将多列数据转化为一列
[root@linuxprobe test2]# echo "1 2 3 4 5 6" > a.txt [root@linuxprobe test2]# cat a.txt 1 2 3 4 5 6 [root@linuxprobe test2]# sed 's/ /\n/g' a.txt ## 将空格替换为换行符,g表示全局 1 2 3 4 5 6
[root@linuxprobe test2]# seq 10000 | tr "\n" " " | sed 's/$/\n/' > a.txt ## 测试多列的情况 [root@linuxprobe test2]# wc -l a.txt ; awk '{print NF}' a.txt 1 a.txt 10000 [root@linuxprobe test2]# time sed 's/ /\n/g' a.txt | wc -l ## 10000列,快 10001 real 0m0.003s user 0m0.002s sys 0m0.002s [root@linuxprobe test2]# seq 100000 | tr "\n" " " | sed 's/$/\n/' > a.txt [root@linuxprobe test2]# wc -l a.txt ; awk '{print NF}' a.txt 1 a.txt 100000 [root@linuxprobe test2]# time sed 's/ /\n/g' a.txt | wc -l ## 100000列,快 100001 real 0m0.014s user 0m0.011s sys 0m0.004s
[root@linuxprobe test2]# cat a.txt a.txt > b.txt [root@linuxprobe test2]# wc -l b.txt ; awk '{print NF}' b.txt ## 测试多行多列的情况 2 b.txt 100000 100000 [root@linuxprobe test2]# time sed 's/ /\n/g' b.txt | wc -l ## 100000列2行,速度快 200002 real 0m0.026s user 0m0.025s sys 0m0.002s [root@linuxprobe test2]# cat a.txt a.txt a.txt a.txt a.txt > b.txt [root@linuxprobe test2]# wc -l b.txt ; awk '{print NF}' b.txt 5 b.txt 100000 100000 100000 100000 100000 [root@linuxprobe test2]# time sed 's/ /\n/g' b.txt | wc -l ##100000列5行,速度快 500005 real 0m0.059s user 0m0.054s sys 0m0.007s
### 每替换一行,行数多出1,末行为空行,sed '/^[\t ]*$/d' file 删除即可
3、利用tr将多列转换为一列
[root@linuxprobe test2]# echo "1 2 3 4 5 6" > a.txt [root@linuxprobe test2]# ls a.txt [root@linuxprobe test2]# cat a.txt 1 2 3 4 5 6 [root@linuxprobe test2]# cat a.txt | tr " " "\n" ##直接替换 1 2 3 4 5 6
[root@linuxprobe test2]# seq 100000 | tr "\n" " " | sed 's/$/\n/' > a.txt ## 测试1行100000列 [root@linuxprobe test2]# wc -l a.txt 1 a.txt [root@linuxprobe test2]# awk '{print NF}' a.txt 100000 [root@linuxprobe test2]# time tr " " "\n" < a.txt | wc -l ## 快 100001 real 0m0.002s user 0m0.003s sys 0m0.000s [root@linuxprobe test2]# seq 1000000 | tr "\n" " " | sed 's/$/\n/' > a.txt ## 1000000列 [root@linuxprobe test2]# wc -l a.txt 1 a.txt [root@linuxprobe test2]# awk '{print NF}' a.txt 1000000 [root@linuxprobe test2]# time tr " " "\n" < a.txt | wc -l ##快 1000001 real 0m0.009s user 0m0.005s sys 0m0.005s
[root@linuxprobe test2]# cat a.txt a.txt a.txt a.txt a.txt > b.txt ## 测试5行1000000列 [root@linuxprobe test2]# wc -l a.txt 1 a.txt [root@linuxprobe test2]# cat a.txt a.txt a.txt a.txt a.txt > b.txt [root@linuxprobe test2]# wc -l b.txt 5 b.txt [root@linuxprobe test2]# awk '{print NF}' b.txt 1000000 1000000 1000000 1000000 1000000 [root@linuxprobe test2]# time tr " " "\n" < b.txt | wc -l ##快 5000005 real 0m0.030s user 0m0.028s sys 0m0.030s
4、利用awk将多列转换为1列
[root@linuxprobe test2]# echo "1 2 3 4 5 6" > a.txt [root@linuxprobe test2]# cat a.txt 1 2 3 4 5 6 [root@linuxprobe test2]# awk '{gsub(" ","\n");print}' a.txt 1 2 3 4 5 6
[root@linuxprobe test2]# seq 100000 | tr "\n" " " | sed 's/$/\n/' > a.txt ##测试多列的情况 [root@linuxprobe test2]# wc -l a.txt ;awk '{print NF}' a.txt 1 a.txt 100000 [root@linuxprobe test2]# time awk '{gsub(" ","\n");print}' a.txt | wc -l ##快 100001 real 0m0.014s user 0m0.013s sys 0m0.002s [root@linuxprobe test2]# seq 1000000 | tr "\n" " " | sed 's/$/\n/' > a.txt [root@linuxprobe test2]# wc -l a.txt ;awk '{print NF}' a.txt 1 a.txt 1000000 [root@linuxprobe test2]# time awk '{gsub(" ","\n");print}' a.txt | wc -l ##快 1000001 real 0m0.123s user 0m0.117s sys 0m0.011s [root@linuxprobe test2]# cat a.txt a.txt a.txt a.txt a.txt > b.txt ##测试多行多列 [root@linuxprobe test2]# wc -l b.txt ;awk '{print NF}' b.txt 5 b.txt 1000000 1000000 1000000 1000000 1000000 [root@linuxprobe test2]# time awk '{gsub(" ","\n");print}' b.txt | wc -l ## 快 5000005 real 0m0.586s user 0m0.579s sys 0m0.031s
### 每替换一行,行数多出1,末行为空行,sed '/^[\t ]*$/d' file 删除即可
内容总结
以上是互联网集市为您收集整理的linux系统中如何将多列数据转化为一列数据全部内容,希望文章能够帮你解决linux系统中如何将多列数据转化为一列数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。