I have the following in a file Output.dat. I need to extract the value between dn: uid= and ,ou= dn: uid=user1,ou=Active,ou=Member,dc=domain,dc=org dn: uid=user2@abc.com,ou=Active,ou=Member,dc=domain,dc=org dn: uid=usertest,ou=Active,ou=Member,dc=domain,dc=org dn: uid=abc1,ou=Active,ou=Member,dc=domain,dc=orgI tried using `sed -e '/dn: uid=/,/,ou=/p' output.dat but it returns complete line instead of value. when tried to use sed -e '/dn: uid=/,/,ou=/\1/p' output.dat then got the following error:     sed: -e expression #1, char 18: unknown command: `\'
extract value between two search patterns on same line
text processing;sed
If you have a version of GNU grep with PCRE (-P) support, then assuming you mean the first occurrence of ,ougrep -oP '(?<=dn: uid=).+?(?=,ou=)' fileIf you want to match up to the second ,ou you can remove the non-greedy ? modifiergrep -oP '(?<=dn: uid=).+(?=,ou=)' fileThe 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' fileor simplifying slightlysed -rn 's/.*dn: uid=([^,]+),ou=.*/\1/p' fileNote 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}' fileor a pure-bash solution such as while IFS=, read -r a b c d; do printf '%s\n' ${a#*=}; done < file 