1、测试数据
root@DESKTOP-1N42TVH:/home/test/test# cat test.txt 2013 2014 2013 2014 1 1.3 0 0 0.9 1.7 0 0 0.9 1.3 4.2 0.9 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0
2、awk实现只匹配0的行
root@DESKTOP-1N42TVH:/home/test/test# cat test.txt 2013 2014 2013 2014 1 1.3 0 0 0.9 1.7 0 0 0.9 1.3 4.2 0.9 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0 root@DESKTOP-1N42TVH:/home/test/test# awk '/^0[^.]/ || /[\t ]0[^.]/' test.txt 1 1.3 0 0 0.9 1.7 0 0 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0
root@DESKTOP-1N42TVH:/home/test/test# cat test.txt 2013 2014 2013 2014 1 1.3 0 0 0.9 1.7 0 0 0.9 1.3 4.2 0.9 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0 root@DESKTOP-1N42TVH:/home/test/test# awk '/^0[\t ]/ || /[\t ]0[\t ]/' test.txt 1 1.3 0 0 0.9 1.7 0 0 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0
root@DESKTOP-1N42TVH:/home/test/test# cat test.txt 2013 2014 2013 2014 1 1.3 0 0 0.9 1.7 0 0 0.9 1.3 4.2 0.9 1 1.6 0 0.9 0 1.6 3 0.9 0.9 1.2 0 0 0.9 1.3 0 0 0.9 1.3 8 0 root@DESKTOP-1N42TVH:/home/test/test# awk '/^0[^.]/ || /\t0[^.]/ || /\t0$/' test.txt 1 1.3 0 0 0.9 1.7 0 0 1 1.6 0 0.9 0 1.6 3 0.9 0.9 1.2 0 0 0.9 1.3 0 0 0.9 1.3 8 0
3、grep实现
root@DESKTOP-1N42TVH:/home/test/test# cat test.txt
2013 2014 2013 2014
1 1.3 0 0
0.9 1.7 0 0
0.9 1.3 4.2 0.9
1 1.6 0 0.9
0 1.6 3 0.9
0.9 1.2 0 0
0.9 1.3 0 0
0.9 1.3 8 0
root@DESKTOP-1N42TVH:/home/test/test# sed 's/\t/ /g' test.txt > test2.txt
root@DESKTOP-1N42TVH:/home/test/test# ls
test.txt test2.txt
root@DESKTOP-1N42TVH:/home/test/test# cat test2.txt
2013 2014 2013 2014
1 1.3 0 0
0.9 1.7 0 0
0.9 1.3 4.2 0.9
1 1.6 0 0.9
0 1.6 3 0.9
0.9 1.2 0 0
0.9 1.3 0 0
0.9 1.3 8 0
root@DESKTOP-1N42TVH:/home/test/test# grep -E "^0[^.]| 0 | 0$" test2.txt
1 1.3 0 0
0.9 1.7 0 0
1 1.6 0 0.9
0 1.6 3 0.9
0.9 1.2 0 0
0.9 1.3 0 0
0.9 1.3 8 0
b、grep命令实现
root@DESKTOP-1N42TVH:/home/test# ls a.txt root@DESKTOP-1N42TVH:/home/test# cat a.txt 2013 2014 2013 2014 1 1.3 0 0 0.9 1.7 0 0 0.9 1.3 4.2 0.9 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0 root@DESKTOP-1N42TVH:/home/test# cat -A a.txt 2013 2014 2013 2014$ 1 1.3 0 0$ 0.9 1.7 0 0$ 0.9 1.3 4.2 0.9$ 1 1.6 0 0.9$ 0 1.6 0 0.9$ 0.9 1.2 0 0$ 0.9 1.3 0 0$ root@DESKTOP-1N42TVH:/home/test# grep $'[\t ]\+0' a.txt ## 提取0前面有空格或者制表符的行 1 1.3 0 0 0.9 1.7 0 0 0.9 1.3 4.2 0.9 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0 root@DESKTOP-1N42TVH:/home/test# grep $'[\t ]\+0' a.txt | grep $'0[\t ]\+' ## 利用管道,增加提取0后面有空格或者制表符的行 1 1.3 0 0 0.9 1.7 0 0 1 1.6 0 0.9 0 1.6 0 0.9 0.9 1.2 0 0 0.9 1.3 0 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。