string - Bash 用 grep 提取字符串
问题描述
假设我在一个文件中有多个字符串,我只想提取一个特定的字符串
$plugin->component = 'mod_jitsi';
$plugin->component = 'local_hvp';
$plugin->component = 'test_bot';
$plugin->component = 'mod_bot';
$plugin->component = 'mod_moodle';
我想用 grep 过滤它,所以我的输出如下所示:
mod
local
test
mod
mod
有什么办法可以用 grep 做到这一点,还是我需要使用 awk 或 sed?
提前致谢!
解决方案
使用 GNUgrep
和 pcre 正则表达式:
grep -Po "(?<== ')[^_]*" input.txt
(?<== ')
是一个零宽度的正向后视断言。它不包含在匹配的文本中,但它必须与= '
包含的 RE 部分之前匹配(即从引号之后到第一个下划线的所有内容。
推荐阅读
- c++ - C++ 参考如何在幕后工作
- android - android.system.ErrnoException: ioctl failed: EBADF (Bad file descriptor) with file from data/data 文件夹
- android - 操作栏不可见?
- javascript - 在反应 DRY 中稍微适应了 HOC
- swift - 如何使用 Google idToken、accessToken 从我的托管服务器到我的应用程序?
- php - php-cs-fixer : 保持大括号在函数声明的同一行
- c# - .NET 的 Ghostscript 版本
- laravel-5 - 对数组使用同步方法并添加额外的数据透视字段?
- reactjs - 传递数据
- firebase - 如何立即在从相机拍摄的屏幕上显示图像?