Grep with SubString

Refer: https://stackoverflow.com/questions/38270261/how-to-extract-a-string-from-between-two-patterns-in-bash

Looking for parameter between -Dappdynamics.agent.nodeName= and closing space

$ ps aux | grep -i appdynamics | grep -oP '(?<= -Dappdynamics.agent.nodeName=)[^ ]+'

$ grep -oP '(?<= -Dappdynamics.agent.nodeName=)[^ ]+' input.txt

-o tells grep to only output the matching part(s) of each line.

-P activates support for PCREs (Perl-compatible regular expressions), which support look-behind assertions such as (?<= inet:), which allow a sub-expression (inet:, in this case) to participate in matching, without being captured (returned) as part of the matched string. [^ ]+ then simply captures everything after inet: up to the first space char. (character set [^ ] matches any char. that is not (^) a space, and + matches this set 1 or more times).

Leave a Reply