Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help a newbie with an awk script

Status
Not open for further replies.

kmfdm515

Technical User
Jun 17, 2004
6
US
Ok, heres a sample file that I need to make a script for:

a
b
c
d
creatorsname = bob
a
b
c
d
e
f
creatorsname = bob
a
b
c
d
creatorsname = bill
a
b
c
d
e
f
g
creatorsname = bob
a
b
c
d
e
creatorsname = jack


What I need to do is pull out every field 'a','b','c' and 'd, but only for certain names (every a,b,c and d field that has a creatorsname = to 'bob', then format the output into different fields so it could be converted to a .csv file - i.e.:

a b c d
a b c d
a b c d

I'm totally new to awk so I don't really know how to go about it. Also, the name comes AFTER the fields a,b,c and d that it pertains to, so would it have to do the search starting from the bottom of the file?

Thanks in advance... :)
 
This may get you started

awk -f kmfdm515.awk infile

#kmfdm515.awk
/creatorsname/{
if ($3 == "bob") {
for (j=1;j<n;j++) printf a[j] " "
print ""
}
n = 0
next
}
{
a[++n] = $0
}

CaKiwi
 
Sorry, typo, needed <= not <

/creatorsname/{
if ($3 == "bob") {
for (j=1;j<=n;j++) printf a[j] " "
print ""
}
n = 0
next
}
{
a[++n] = $0
}

CaKiwi
 
Thanks CaKiwi, but I guess I could have saved you some trouble by posting a snip of the actual file I need to parse - I apologize. I figured I could take what you gave me and make the necessary changes, but I'm too new to this.

Here's what the file would have:

cn: bob
mail: bob@domain.com
givenName: Robert
sn: James
creatorsName: cn=info,dc=testdomain,dc=domain,dc=net
createTimestamp: 20040319135044Z
cn: tom
mail: tom@domain.com
givenName: Thomas
sn: James
phone: 555-555-5555
zip: 48317
creatorsName: cn=info,dc=testdomain,dc=domain,dc=net
createTimestamp: 20040319135044Zcn: tom
cn: bill
mail: bill@domain.com
givenName: William
sn: James
creatorsName: cn=test,dc=testdomain,dc=domain,dc=net
createTimestamp: 20040319135044Z


so it would take the 'cn', 'mail', 'givenName' and 'sn' fields and paste them into the new file, but only if the 'creatorsName' that followed was 'cn=info'...so you'd have:

bob bob@domain.com Robert James
tom tom@domain.com Tom James

...leaving out the last entry because it was a different 'creatorsName', and also leaving out the 'phone' and 'zip' fields.

If you're brave enough to tackle that, thanks....

 
Try this. I also changed it to print commas between the fields

/^cn/ || /^mail/ || /givenName/ || /^sn/ {
a[++n] = $0
next
}
/creatorsName/{
if (/cn=info/) {
for (j=1;j<n;j++) printf a[j] ","
if (j==n) print a[j]
}
n = 0
}


CaKiwi
 
Sorry, I was a bit hasty. my script prints out the wjhole line instead of just the second field. Try this

/^cn/ || /^mail/ || /^givenName/ || /^sn/ {
a[++n] = $2
next
}
/creatorsName/{
if (/cn=info/) {
for (j=1;j<n;j++) printf a[j] ","
if (j==n) print a[j]
}
n = 0
}

CaKiwi
 
That seemed to work... but sometimes it puts the 'mail:' field on the line below. I have no idea why just this field would get thrown down into a new line.
 
This the output I got

bob,bob@domain.com,Robert,James
tom,tom@domain.com,Thomas,James

Maybe there is a carriage return or some other control character on the mail line. Try

od -c inputfile

to check it.

CaKiwi
 
Ugh. Nevermind. These address books have no rhyme or reason. It seems sometimes the 'mail' field comes AFTER the creatorsName field...which would make this pretty much impossible to search, whether you go bottom to top or vice versa.

Thanks for the scripts anyways....they helped give me a base for how you can script with awk. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top