首页 > 解决方案 > 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.

awk/sed/grep to extract the desired string

标签: regexbashawksed

解决方案


正则表达式的第一部分是可变宽度的正向后视:

(?<=
  TOPIC \s*\('|
  QLOCAL\s*\('|
  QALIAS\s*\('|
  SUB   \s*\('
)

建议将文字单词合并到它自己的交替组中,并使用一个\K运算符来丢弃从当前内存缓冲区到目前为止匹配的文本:(?:TOPIC|QLOCAL|QALIAS|SUB)\s*\('\K.

其余的可以与-oPoptions 和 GNU一起使用grep

grep -oP "(?:TOPIC|QLOCAL|QALIAS|SUB)\s*\('\K.*?(?='\))" file

请参阅正则表达式演示在线grep演示

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

推荐阅读