Ashish Kumar
Ashish Kumar

Reputation: 544

grep with regex pattern recursively

Regex pattern to search all recurences in string.

Ex- echo '%%MYSQL_PORT%%=%%3356%%' | grep \%%.*\%%

Actual output- %%MYSQL_PORT%%=%%3356%%

Expected output- %%MYSQL_PORT%% %%3356%%

Upvotes: 1

Views: 200

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627101

You may use

echo '%%MYSQL_PORT%%=%%3356%%' | grep -o '%%[^%]*%%'

See the online demo

You need -o option to output the matches only and you should replace .* with [^%]* that will only match 0 or more chars other than % char.

Output:

%%MYSQL_PORT%%
%%3356%%

Upvotes: 2

Related Questions