If you have a version of GNU grep with PCRE (-P
) support, then assuming you mean the first occurrence of ,ou
grep -oP '(?<=dn: uid=).+?(?=,ou=)' file
If you want to match up to the second ,ou
you can remove the non-greedy ?
modifier
grep -oP '(?<=dn: uid=).+(?=,ou=)' file
The expressions in parentheses are zero-length assertions (aka lookarounds) meaning that they form part of the match, but are not returned as part of the result. You could do the same thing natively in perl e.g.
perl -ne 'print "$1\n" if /(?<=dn: uid=)(.+?)(?=,ou=)/' file
It's possible to do something similar in sed, using regular (non zero-length) grouping e.g. (for GNU sed - other varieties may need additional escaping)
sed -rn 's/(.*dn: uid=)([^,]+)(,ou=.*)/\2/p' file
or simplifying slightly
sed -rn 's/.*dn: uid=([^,]+),ou=.*/\1/p' file
Note the [^,]
is a bit of a hack here, since sed doesn't have a true non-greedy match option.
Afterthought: although it's not exactly what you asked, it looks like what you actually want to do is read comma-separated name=value
pairs from a file, and then further split the value of the first field from its name. You could achieve that in many ways - including
awk -F, '{sub(".*=","",$1); print $1}' file
or a pure-bash solution such as
while IFS=, read -r a b c d; do printf '%s\n' "${a#*=}"; done < file