regex - how to extract the string from two delimiter strings using sed or awk or bash?
问题描述
I'm not that frequent with awk or sed. Appreciate your assistance for below:
I have a bash shell variable $VAR with value :
DEFINE QLOCAL ('IIB.TESTQUEUE.MODULE') +
DESCR('Input queue for A to B') +
LIKE('MY.LOCALQ.TEMP') +
REPLACE
I want to extract the value IIB.TESTQUEUE.MODULE
between QLOCAL<0 or more spaces>('
and ')
from the shell variable $VAR.
I framed the below regex based on other existing questions and an online regex tool but this is not printing anything when i try embed this regex in awk or sed.
解决方案
正则表达式的第一部分是可变宽度的正向后视:
(?<=
TOPIC \s*\('|
QLOCAL\s*\('|
QALIAS\s*\('|
SUB \s*\('
)
建议将文字单词合并到它自己的交替组中,并使用一个\K
运算符来丢弃从当前内存缓冲区到目前为止匹配的文本:(?:TOPIC|QLOCAL|QALIAS|SUB)\s*\('\K
.
其余的可以与-oP
options 和 GNU一起使用grep
:
grep -oP "(?:TOPIC|QLOCAL|QALIAS|SUB)\s*\('\K.*?(?='\))" file
s="DEFINE QLOCAL ('IIB.TESTQUEUE.MODULE') +
DESCR('Input queue for A to B') +
LIKE('MY.LOCALQ.TEMP') +
REPLACE"
grep -oP "(?:TOPIC|QLOCAL|QALIAS|SUB)\s*\('\K.*?(?='\))" <<< "$s"
# => IIB.TESTQUEUE.MODULE
推荐阅读
- python-3.x - 资源分配算法 - 给定时间最小化成本
- c# - Visual Studio 发布控制台应用程序自动更新
- python - Paramiko SSH 连接无法使用 tensorflow 访问 GPU
- javascript - 如何应用自定义断点(Material UI)?
- javascript - 如果使用“固定”的内容触及特定位置(类),我希望该位置是固定的
- amazon-web-services - AWS IoT:多种事物的证书
- ffmpeg - FFMPEG 中的 rtsp_mpegts、mpegts、h264 选项有什么区别?
- json - “List”类没有名为“fromJson”的构造函数。尝试调用不同的构造函数,或定义一个名为“fromJson”的构造函数
- mysql - Mysql - 无法更改或添加新密码
- javascript - 在 SummorNote 中选择、拖放文本会引发 'Uncaught TypeError: Cannot read properties of null (reading 'insertBefore')' 异常