Reputation: 534
I'm trying to grep the occurrences for 2 scenarios: 1. before the 1st space 2. before the end of the line
Example
-Dcatalina.base=/apps/kio/jal/current/mi_loki_porf -Dcatalina.home=/apps/kio/jal/current/mi_loki_porf
-Djava.io.tmpdir=/apps/kio/jal/current/mi_loki_porf/temp org.apache.catalina.startup.Bootstrap start
I wanted to extract the occurences after the word '-Dcatalina.base=' before the 1st space and that would be /apps/kio/jal/current/mi_loki_porf
Extract the occurences after the word '-Dcatalina.base' but before the end of line and that would be /apps/kio/jal/current/mi_loki_porf -Dcatalina.home=/apps/kio/jal/current/mi_loki_porf
Also, how should I approach if I've no idea about the occurences after the match, say ocurrences after '-Dcatalina.base=' can be words or alphanumeric or can be only special characters
-Dcatalina.base=*(76yhg5)
or -Dcatalina.base=123345
or -Dcatalina.base=hellohowareyou
or -Dcatalina.base= *&^%$
What I've tried
grep -Po '(?<=-Dcatalina.base=).*' my.log
and it gives me the entire match after -Dcatalina.base
Upvotes: 0
Views: 56
Reputation: 91518
Using perl, you can do:
perl -nE '/(?<=-Dcatalina\.base=)(\S+).*/ && say $1,"\n",$&' my.log
This will apply the regex on every line of the file my.log
and print if it matches.
Explanation:
/ : regex delimiter
(?<= : positive lookbehind
-Dcatalina\.base= : literally
) : end lookbehind
(\S+) : group 1, 1 or more non space
.* : 0 or more any character but newline
/ : regex delimiter
&& : logical and, the following is executed only if the regex matches
say $1,"\n",$& : print content of group 1, a line break and the whole match
Result for given example:
/apps/kio/jal/current/mi_loki_porf
/apps/kio/jal/current/mi_loki_porf -Dcatalina.home=/apps/kio/jal/current/mi_loki_porf
Upvotes: 1