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