Skip to main content
With GNU `grep`, you need the `--` even though there is a non-option argument.
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Use GNU grep with PCRE:

grep -Po -- '(?<=user=).+?(?=,)' *.log

If you like to display the user= keyword too:

grep -o -- 'user=[^,]*' *.log

you can even display the file in which the match succeeded by adding the -H option to the grep command above (without it, the file name is only displayed if there is more than one file specified).

So the grep command become:

grep -Ho -- 'user=[^,]*' *.log

And the output:

pgsql.log:user=postgres
pgsql.log:user=postgres

Use GNU grep with PCRE:

grep -Po '(?<=user=).+?(?=,)' *.log

If you like to display the user= keyword too:

grep -o 'user=[^,]*' *.log

you can even display the file in which the match succeeded by adding the -H option to the grep command above.

So the grep command become:

grep -Ho 'user=[^,]*' *.log

And the output:

pgsql.log:user=postgres
pgsql.log:user=postgres

Use GNU grep with PCRE:

grep -Po -- '(?<=user=).+?(?=,)' *.log

If you like to display the user= keyword too:

grep -o -- 'user=[^,]*' *.log

you can even display the file in which the match succeeded by adding the -H option to the grep command above (without it, the file name is only displayed if there is more than one file specified).

So the grep command become:

grep -Ho -- 'user=[^,]*' *.log

And the output:

pgsql.log:user=postgres
pgsql.log:user=postgres
Source Link

Use GNU grep with PCRE:

grep -Po '(?<=user=).+?(?=,)' *.log

If you like to display the user= keyword too:

grep -o 'user=[^,]*' *.log

you can even display the file in which the match succeeded by adding the -H option to the grep command above.

So the grep command become:

grep -Ho 'user=[^,]*' *.log

And the output:

pgsql.log:user=postgres
pgsql.log:user=postgres