unix - 如何使用 Unix 从一行中删除第二次出现
问题描述
我在文件中有以下数据
They used to carry lots of treats
but now they don’t have any sweets.
And so, if you’re in need of candy,
please don’t visit Mrs. Mandy. He is working in office.But will be late from office
例如 :
使用以下命令,我得到了作为同一行中的两个关键字的行
grep "Mandy" file.txt | grep "office"
上面的命令给了我下面的行作为输出
please don’t visit Mrs. Mandy. He is working in office.But will be late from office
现在从上面我得到的作为输出的行需要删除第二个出现的关键字:office
并在输出下方打印
预期输出:
please don’t visit Mrs. Mandy. He is working in office.But will be late from
如何实现上述预期输出?
解决方案
我认为这就是你要找的:
grep "Mandy" file.txt | grep "office" | grep -oP '.*(?=office)(?=office)'
这使用正则表达式和积极的前瞻。它产生您正在寻找的输出:
please don’t visit Mrs. Mandy. He is working in office.But will be late from
推荐阅读
- python - 如何使用 PyQt5 运行 while 循环
- sql - 如何从现有表记录创建视图,同时添加不存在的新记录
- node.js - 尝试在终端中使用 geoproject 命令时抛出错误
- apache-kafka - kafka Connect:Tasks.max 超过 # 个分区,但状态显示为 RUNNING
- typescript - Typescript 使用 Absolute Import Linting 测试问题
- javascript - 在 Javascript 中查找不包含/indexOf/Regex 的字符串中的子字符串
- servicestack - 将连接字符串从 Startup 传递到任何其他控制器的正确方法
- sql - Azure SQL 垂直弹性查询教程错误
- javascript - 如何为 Stripe 的“RateLimitError”和“StripePermissionError”错误编写测试?
- r - Different scale for faceted plots: scale_y_continuous()