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

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 下写批处理


下一篇:linux文本处理之 grep