【发布时间】:2023-03-20 06:07:01
【问题描述】:
假设我在一个文件中有多个字符串,我只想提取一个特定的字符串
$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?
提前致谢!
【问题讨论】:
-
$plugin->component =是否在所有行中都保持不变,并且您想提取字符串其余部分中直到第一个_的所有内容? -
awk无论如何都更清楚:awk -F "['_]" '{print $2}' file