I have the following ldif:
dn: cn=Robert Smith,ou=people,dc=example,dc=com
objectclass: inetOrgPerson
cn: Robert Smith
cn: Robert J Smith
cn: bob smith
sn: smith
uid: rjsmith
userpassword: rJsmitH
carlicense: HISCAR 123
homephone: 555-111-2222
mail: [email protected]
alias: [email protected]
alias: [email protected]
description: nice hair
ou: Human Resources
dn: cn=John Doe,ou=people,dc=example,dc=com
objectclass: inetOrgPerson
cn: John Doe
cn: John Walker Doe
cn: Johnny
sn: Doe
uid: jdoe
userpassword: topsecret
carlicense: AKAHH 123
homephone: 123-458-362
mail: [email protected]
alias: [email protected]
alias: [email protected]
description: cool guy
ou: Sales
Now I am running an awk command against it:
awk '/^mail:/ { mail = $2 }; {print mail };' ldif
The expected result would be:
[email protected]
[email protected]
The actual result is:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
I don't really understand why awk gives this output multiple times. I'd highly appreciate it if someone could explain it to me since I am new to awk and haven't used it much before. I already consulted the man page and Google, but I guess I am looking for the wrong things there...
EDIT: I understand that awk processes text streams in lines. I guess my "print" simply prints the output as often as there are lines in my ldif file. But how can I prevent awk from doing that? I only want to print each result once..