linux系统中如何精确匹配0
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2021-01-01 python中删除字典的键值对
2021-01-01 python中清空字典
2021-01-01 python中由列表生成字典
2021-01-01 python中zip同时循环两个可迭代对象
2021-01-01 python中提取字典的指定键值对
2021-01-01 python中字典顺序逆转
2021-01-01 python中字典的复制