awk if分支拆分为不同的文件

 

[root@pc1 test1]# ls
file.txt
[root@pc1 test1]# cat file.txt            ## 测试文件
0 01 02 03 04
0 05 06 07 08
0 09 10 11 12
1 13 14 15 16
1 17 18 19 20
2 77 33 22 66
[root@pc1 test1]# awk '{if ($1 == "0") print $0 > "a.txt"; else if ($1 == "1") print $0 > "b.txt"}' file.txt
[root@pc1 test1]# ls                      ## 不同的分支输出为各自的文件
a.txt  b.txt  file.txt
[root@pc1 test1]# cat a.txt       ## 分支1
0 01 02 03 04
0 05 06 07 08
0 09 10 11 12
[root@pc1 test1]# cat b.txt       ## 分支2
1 13 14 15 16
1 17 18 19 20

 

 

[root@pc1 test1]# ls
file.txt
[root@pc1 test1]# awk '{if ($1 == 0) print $0 > "a.txt"; else if ($1 == 1) print $0 > "b.txt"; else print $0 > "c.txt"}' file.txt
[root@pc1 test1]# ls                                      ## 拆分为3个文件
a.txt  b.txt  c.txt  file.txt
[root@pc1 test1]# cat file.txt
0 01 02 03 04
0 05 06 07 08
0 09 10 11 12
1 13 14 15 16
1 17 18 19 20
2 77 33 22 66
[root@pc1 test1]# cat a.txt           ## 分支1
0 01 02 03 04
0 05 06 07 08
0 09 10 11 12
[root@pc1 test1]# cat b.txt            ## 分支2
1 13 14 15 16
1 17 18 19 20
[root@pc1 test1]# cat c.txt            ## 分支3
2 77 33 22 66

 

 

。 

 

posted @ 2023-10-12 10:37  小鲨鱼2018  阅读(29)  评论(0编辑  收藏  举报