Linux grep技巧 删除含有指定关键词的行,创建新文件

一. 需求

⏹有如下文件,现要求


二. grep -v 排除

⏹方式1

  • -E:用来指定正则表达式
  • -v:用来排除
bash 复制代码
grep -E "xuecheng|192.168.1.1" -v ./hosts
bash 复制代码
# 查看hosts文件内容
fengyehong@ubuntu:~/jmw_work_space/20240625$ cat hosts
127.0.0.1 www.xuecheng.com
127.0.0.1 img.xuecheng.com
192.168.1.1 www.test.com
127.0.0.1 video.xuecheng.com
127.0.0.1 teacher.xuecheng.com
127.0.0.1 system.xuecheng.com
127.0.0.1 ucenter.xuecheng.com
127.0.0.1 license.sublimehq.com

# 排除指定的行
fengyehong@ubuntu:~/jmw_work_space/20240625$ grep -E "xuecheng|192.168.1.1" -v ./hosts
127.0.0.1 license.sublimehq.com

⏹方式2

bash 复制代码
grep -v "xuecheng" ./hosts | grep -v "192.168.1.1"
bash 复制代码
# 查看hosts文件内容
fengyehong@ubuntu:~/jmw_work_space/20240625$ cat hosts
127.0.0.1 www.xuecheng.com
127.0.0.1 img.xuecheng.com
192.168.1.1 www.test.com
127.0.0.1 video.xuecheng.com
127.0.0.1 teacher.xuecheng.com
127.0.0.1 system.xuecheng.com
127.0.0.1 ucenter.xuecheng.com
127.0.0.1 license.sublimehq.com

# 先使用-v排除掉xuecheng关键字之后,再使用-v排除192.168.1.1关键字
fengyehong@ubuntu:~/jmw_work_space/20240625$ grep -v "xuecheng" ./hosts | grep -v "192.168.1.1"
127.0.0.1 license.sublimehq.com

三. 创建新文件

bash 复制代码
grep -v "xuecheng" ./hosts | grep -v "192.168.1.1" > new_hosts
相关推荐
花嫁代二娃5 小时前
Linux:环境变量
linux
乌托邦的逃亡者6 小时前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
ldj20206 小时前
Jenkins 流水线配置
运维·jenkins
古希腊数通小白(ip在学)8 小时前
stp拓扑变化分类
运维·服务器·网络·智能路由器
l1x1n010 小时前
Vim 编辑器常用操作详解(新手快速上手指南)
linux·编辑器·vim
12点一刻11 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务11 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
ajassi200012 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
o不ok!12 小时前
Linux面试问题-软件测试
linux·运维·服务器
DaxiaLeeSuper12 小时前
Prometheus+Grafana+node_exporter监控linux服务器资源的方案
linux·grafana·prometheus