linux系统中如何将多行数据转化为一行数据


1、利用xargs进行行列转换 

[root@linuxprobe test2]# seq 5
1
2
3
4
5
[root@linuxprobe test2]# seq 5 | xargs ## xargs 默认转化为1行
1 2 3 4 5
[root@linuxprobe test2]# seq 5000 | xargs | wc -l  
1
[root@linuxprobe test2]# seq 50000 | xargs | wc -l  ## 当行数过多时,xargs不适用
3

 

2、数据有多列数据时xargs仍然适用

[root@linuxprobe test2]# paste <(seq 6) <(seq 6) <(seq 6) > a.txt ## 创建测试数据 [root@linuxprobe test2]# cat a.txt 1       1       1
2       2       2
3       3       3
4       4       4
5       5       5
6       6       6 [root@linuxprobe test2]# cat a.txt | xargs ## xargs将数据自左至右排成一行 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6
[root@linuxprobe test2]# paste <(seq 60000) <(seq 60000) <(seq 60000) > b.txt ## 创建较大的测试数据 [root@linuxprobe test2]# ls b.txt [root@linuxprobe test2]# wc -l b.txt 60000 b.txt [root@linuxprobe test2]# head b.txt 1       1       1
2       2       2
3       3       3
4       4       4
5       5       5
6       6       6
7       7       7
8       8       8
9       9       9
10      10      10 [root@linuxprobe test2]# cat b.txt | xargs | wc -l ## 数据较大时不能转化为一行 8

 

3、xargs可以指定输出的列数

[root@linuxprobe test2]# seq 10 | xargs 1 2 3 4 5 6 7 8 9 10 [root@linuxprobe test2]# seq 10 | xargs -n 2 ##利用 -n 参数指定输出的列数
1 2
3 4
5 6
7 8
9 10 [root@linuxprobe test2]# seq 10 | xargs -n 3
1 2 3
4 5 6
7 8 9
10 [root@linuxprobe test2]# seq 10 | xargs -n 4
1 2 3 4
5 6 7 8
9 10
[root@linuxprobe test2]# seq 1000000 | xargs -n 2000 | awk '{print NF}' | head -n 3 ## 指定列数,并查看列数
2000
2000
2000 [root@linuxprobe test2]# seq 1000000 | xargs -n 20000 | awk '{print NF}' | head -n 3
20000
20000
20000 [root@linuxprobe test2]# seq 1000000 | xargs -n 200000 | awk '{print NF}' | head -n 3 ## 当指定的列数较多时不适用
23695
21844
21844

 

4、利用sed命令将多行转化为一行

[root@linuxprobe test2]# seq 10 > a.txt [root@linuxprobe test2]# cat a.txt 1
2
3
4
5
6
7
8
9
10 [root@linuxprobe test2]# sed 'N;s/\n/ /' a.txt ##N可以实现将两行按照一行处理,将换行符\n替换为空格 1 2
3 4
5 6
7 8
9 10 [root@linuxprobe test2]# sed ':a; N;s/\n/ /; ta' a.txt ## 利用sed的跳转功能 1 2 3 4 5 6 7 8 9 10
[root@linuxprobe test2]# seq 100000 > a.txt [root@linuxprobe test2]# wc -l a.txt 100000 a.txt [root@linuxprobe test2]# time sed ':a; N;s/\n/ /; ta;' a.txt | wc -l ## 不受数据行数限制;但是随行数增加,耗时,行数多慎用 1 real 0m28.725s user 0m28.642s sys 0m0.005s
[root@linuxprobe test2]# paste <(seq 100) <(seq 100) <(seq 100) > a.txt [root@linuxprobe test2]# wc -l a.txt 100 a.txt [root@linuxprobe test2]# head -n 2 a.txt 1       1       1
2       2       2 [root@linuxprobe test2]# time sed ':a; N;s/\n/ /; ta;' a.txt | wc -l ## 数据有多列的情况同样适用 1 real 0m0.002s user 0m0.000s sys 0m0.003s

 

5、利用tr命令将多行数据转化为一行

[root@linuxprobe test2]# seq 10 > a.txt [root@linuxprobe test2]# cat a.txt 1
2
3
4
5
6
7
8
9
10 [root@linuxprobe test2]# cat a.txt | tr "\n" " " ## 利用tr将换行符替换为空格,但是末尾没有换行符
1 2 3 4 5 6 7 8 9 10 [root@linuxprobe test2]# [root@linuxprobe test2]# cat a.txt | tr "\n" " " | sed 's/$/\n/' ## 利用sed命令在末尾添加换行符
1 2 3 4 5 6 7 8 9 10
[root@linuxprobe test2]# time seq 100000 | tr "\n" " " | sed 's/$/\n/' | wc -l ## 测试多行,测试耗时 1 real 0m0.003s user 0m0.002s sys 0m0.005s [root@linuxprobe test2]# time seq 1000000 | tr "\n" " " | sed 's/$/\n/' | wc -l 1 real 0m0.018s user 0m0.012s sys 0m0.019s [root@linuxprobe test2]# time seq 10000000 | tr "\n" " " | sed 's/$/\n/' | wc -l ##不受行数限制,速度快 1 real 0m0.210s user 0m0.012s sys 0m0.298s




[root@linuxprobe test2]# paste <(seq 1000000) <(seq 1000000) <(seq 1000000) > a.txt [root@linuxprobe test2]# wc -l a.txt 1000000 a.txt [root@linuxprobe test2]# awk '{print NF}' a.txt | head -n 3
3
3
3 [root@linuxprobe test2]# time cat a.txt | tr "\n" " " | sed 's/$/\n/' | wc -l ## 数据有多列时仍然适用 1 real 0m0.046s user 0m0.011s sys 0m0.075s

 

6、利用awk命令将多行数据转化为一行

[root@linuxprobe test2]# seq 10 > a.txt [root@linuxprobe test2]# cat a.txt 1
2
3
4
5
6
7
8
9
10 [root@linuxprobe test2]# awk BEGIN{RS=EOF}'{gsub("\n"," ");print}' a.txt ##利用awk将多行转化为一行 1 2 3 4 5 6 7 8 9 10 [root@linuxprobe test2]# time seq 100000 | awk BEGIN{RS=EOF}'{gsub("\n"," ");print}' | wc -l ##测试多行,测试耗时 1 real 0m0.014s user 0m0.014s sys 0m0.003s [root@linuxprobe test2]# time seq 1000000 | awk BEGIN{RS=EOF}'{gsub("\n"," ");print}' | wc -l 1 real 0m0.124s user 0m0.123s sys 0m0.013s [root@linuxprobe test2]# time seq 10000000 | awk BEGIN{RS=EOF}'{gsub("\n"," ");print}' | wc -l ##不受行数限制,时间快 1 real 0m1.243s user 0m1.194s sys 0m0.153s
[root@linuxprobe test2]# paste <(seq 10000000) <(seq 10000000) <(seq 10000000) <(seq 10000000) > a.txt [root@linuxprobe test2]# wc -l a.txt 10000000 a.txt [root@linuxprobe test2]# awk '{print NF}' a.txt | head -n 3
4
4
4 [root@linuxprobe test2]# time awk BEGIN{RS=EOF}'{gsub("\n"," ");print}' a.txt | wc -l ##数据多列的情况适用 1 real 0m9.015s user 0m3.824s sys 0m5.285s

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM