Merging into one regex expression is hard here because POSIX regex does not support lazy quantifiers.
With GNU sed
, you can pass the command as
sed 's/.*FROM \(.*\) as.*/\1/;s/FROM //' file
See this online demo.
However, if you have a GNU grep
you can use a bit more precise expression:
#!/bin/bash
s='FROM some_registry as registry1
From another_registry'
grep -oP '(?i)\bFROM\s+\K.*?(?=\s+as\b|$)' <<< "$s"
See the online demo. Details:
(?i)
- case insensitive matching ON
\b
- a word boundary
FROM
- a word
\s+
- one or more whitespaces
\K
- "forget" all text matched so far
.*?
- any zero or more chars other than line break chars as few as possible
(?=\s+as\b|$)
- a positive lookahead that matches a location immediately followed with one or more whitespaces and then a whole word as
, or end of string.