Monday, July 25, 2022

[SOLVED] grep between pattern and exclude start/end pattern in output

Issue

I have file.txt from which I need to grep first occurrence of a pattern. How can I grep and get matched string only ':whitespace' and 'end of line' I m trying below command

cat file.txt |  grep -m1 -P "(:\s+).*ccas-apache$"

But it gives me

name: nginx-ccas-apache

and I want is

nginx-ccas-apache

file.txt

pod: nginx-ccas-apache-0
        name: nginx-ccas-apache
        image: myregnapq//ccas_apache
        name: nginx-filebeat
pod: nginx-ccas-apache-1
        name: nginx-ccas-apache
        image: myregnapq/ccas_apache
        name: nginx-filebeat

Solution

Another approach using sed:

sed -En '/^[[:space:]]+name:[[:space:]](.*ccas-apache)$/{s//\1/p;q}' file.txt

Explanation

  • -En Use extended regexp with -E and prevent the default printing of a line by sed with -n

  • /^[[:space:]]+name:[[:space:]](.*ccas-apache)$/ The pattern that specifies what to match

  • If the previous pattern matched, run commands between the curly brackets

  • s//\1/p Use the last matched pattern with // and replace with group 1. Then print the pattern space with p

  • q exit sed

The regex matches:

  • ^ Start of string
  • [[:space:]]+name:[[:space:]] Match name: with leading spaces and single space after
  • (.*ccas-apache) Capture group 1, match optional chars and ccas-apache
  • $ End of string

Output

nginx-ccas-apache

Note that you don't have to use cat

See an online demo.



Answered By - The fourth bird
Answer Checked By - Marilyn (WPSolving Volunteer)