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 刪除即可