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 matchIf 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 withp
q
exit sed
The regex matches:
^
Start of string[[:space:]]+name:[[:space:]]
Matchname:
with leading spaces and single space after(.*ccas-apache)
Capture group 1, match optional chars andccas-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)