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

发布时间 2023-10-12 10:46:17作者: 小鲨鱼2018

 

[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
0 01 02 03 04
0 05 06 07 08
0 09 10 11 12
[root@pc1 test1]# cat b.txt
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
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
0 01 02 03 04
0 05 06 07 08
0 09 10 11 12
[root@pc1 test1]# cat b.txt
1 13 14 15 16
1 17 18 19 20
[root@pc1 test1]# cat c.txt
2 77 33 22 66

 

 

。