0

I would like to keep the strings between (FROM and as), and (From and newline character).

Input:

FROM some_registry as registry1
FROM another_registry

Output:

some_registry
another_registry

Using the following sed command, I can extract the strings. Is there a way to combine the two sed commands?

sed -e 's/.*FROM \(.*\) as.*/\1/' | sed s/"FROM "//

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
Marc
  • 588
  • 2
  • 15
  • 1
    `sed 's/.*From \(.*\) as.*/\1/;s/From //' file`? See https://ideone.com/cjvvaB. Merging into one regex expression is hard here because POSIX regex does not support lazy quantifiers. – Wiktor Stribiżew Feb 15 '22 at 09:27
  • That is because your regex does not match the string you posted in the question (see `From` and `FROM` and `"FROM "`). – Wiktor Stribiżew Feb 15 '22 at 09:31
  • Well, maybe `grep -oP 'FROM\s+\K.*?(?=\s+as\b|$)' file` is a better option? If you have a GNU `grep`, of course. – Wiktor Stribiżew Feb 15 '22 at 09:33
  • 1
    From duplicate link, look at the answer https://stackoverflow.com/a/25386918/5866580 and when I run it accordingly to shown samples eg: `grep -oP 'FROM\s*\K(?:(?!\s+as).)*' file` it gives me required output. – RavinderSingh13 Feb 15 '22 at 10:23

1 Answers1

1

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563