Please help in sorting record

dn: uid=peter@exmaple.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton



dn: uid=ras@exmaple.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam

i have a text file [above text] with 300 entries with multiple ldap entries but i want to search specific entries like all users from @example.com email addres with their name as well, like

email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam

Thanks,

like this?

awk -F "[ ,:=]" '/@exmaple.com/ {R=NR;ID=$4}NR==R+3{print "Email:"ID" Name:"$3,$4}' file
$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton
 
dn: uid=ras@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam
 
dn: uid=james@mi6.com,ou=example-com,ou=mail,dc=example,dc=to
cn: James
sn: Bond
displayName: James Bond
 
dn: uid=Bat@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){print $x," Name: ",$1; $x=""}' f3
email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam
email:Bat@example.com Name: Bat Man
$
$
sed -n '/^dn: uid=[^@]*@example\.com/{s/[^=]*=\([^,]*\).*/email:\1/; h; n; n; n; s/display//; H; g; y/\n/ /; p;}' file

email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam

---------- Post updated at 02:40 PM ---------- Previous update was at 02:30 PM ----------

This solution does not work correctly if a matching email address isn't found in the first 3 lines. If that's the case, an unset R evaluates to 0 and NR==R+3 will evalute to true when NR is 3. At that point, unwanted data may print.

Regards,
Alister

Thanks so much, it is perfect, can you suggest how to sort my lines so All [a,A] shows first and [zZ] shows in last, i mean text orderwise on any column better on name basis.

Once again thanks.

$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton
dn: uid=ras@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Ras
sn: Kam
displayName: Ras Kam
dn: uid=james@mi6.com,ou=example-com,ou=mail,dc=example,dc=to
cn: James
sn: Bond
displayName: James Bond
dn: uid=Bat@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$ ##
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){push @a, "$x Name: $1"; $x=""}
>            END {print foreach (sort @a)}' f3
email:Bat@example.com Name: Bat Man
email:peter@example.com Name: Peter Norton
email:ras@example.com Name: Ras Kam
$
$

tyler_durden

Looks like you asked for a case-insensitive sort. Here's an idea -

$
$
$ cat f3
dn: uid=peter@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Peter
sn: Norton
displayName: Peter Norton

dn: uid=adam@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: adam
sn: smith
displayName: adam smith

dn: uid=james@mi6.com,ou=example-com,ou=mail,dc=example,dc=to
cn: James
sn: Bond
displayName: James Bond

dn: uid=Bat@example.com,ou=example-com,ou=mail,dc=example,dc=to
cn: Bat
sn: Man
displayName: Bat Man
$
$
$ perl -lne 'if (/^.*?uid=(.*?\@example.com).*$/){$x="email:$1"}
>            elsif(/^displayName: (.*?)$/ && $x){push @y, "$x Name: $1"; $x=""}
>            END {print foreach (sort {uc($a) cmp uc($b)} @y)}' f3
email:adam@example.com Name: adam smith
email:Bat@example.com Name: Bat Man
email:peter@example.com Name: Peter Norton
$
$

The prior script, when run on this file will put "Bat Man" before "adam smith" because ascii('B') < ascii('a').

HTH,
tyler_durden