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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reporting from a flat file 1

Status
Not open for further replies.

ahmalik

Programmer
May 22, 2007
3
0
0
US
Hi,

I have a file with 1200 entries and each entry is 3 lines as follows and each entry is seperated with a blank line.

dn: uid=Bart.Simpson, ou=People, o=simpson.com
Department: BSW
mailMessageStore: store9

dn: uid=Lisa.Simpson, ou=People, o=simpson.com
Department: CSW
mailMessageStore: store8

This is an output from ldapsearch on a directory server. There are 10 mailMessage Stores on the Directory server and 10 or 11 Departments. Each mailStore has users from the 10-11 different departments. I am trying to put a script together that could tell me the number of users from each Department on a mailStore.
For example, on the directory server mailStore1 has 11 users from BSW, 10 users from CSW and son on. The end result should look like this.

BSW CSW DSW ESW FSW GSW
mailStore1 24 2 100 500 98 65
mailStore2 2 3 6 5 8 6

(Depts) (Number of users in each Dept on a particular mailStore

Any help would be appreciated.

Thanks,
 
Well here is how someone might parse that data. I'll let you or someone else come up with how to format the data.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]$/[/blue] = [red]"[/red][purple][purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%data[/blue][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]dn: uid=(.*), ou=(.*), o=(.*)[purple][b]\n[/b][/purple]Department: (.*)[purple][b]\n[/b][/purple]mailMessageStore: (.*)[/purple][red]/[/red][red])[/red] [red]{[/red]
		[blue]$data[/blue][red]{[/red][blue]$5[/blue][red]}[/red][red]{[/red][blue]$4[/blue][red]}[/red] = [red]{[/red][purple]uid[/purple] => [blue]$1[/blue], [purple]ou[/purple] => [blue]$2[/blue], [purple]o[/purple] => [fuchsia]3[/fuchsia][red]}[/red][red];[/red]
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Unknown data format: [blue]$_[/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[black][b]use[/b][/black] [green]Data::Dumper[/green][red];[/red]
[black][b]print[/b][/black] [maroon]Dumper[/maroon][red]([/red]\[blue]%data[/blue][red])[/red][red];[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__DATA__[/teal]
[teal]dn: uid=Bart.Simpson, ou=People, o=simpson.com[/teal]
[teal]Department: BSW[/teal]
[teal]mailMessageStore: store9[/teal]

[teal]dn: uid=Lisa.Simpson, ou=People, o=simpson.com[/teal]
[teal]Department: CSW[/teal]
[teal]mailMessageStore: store8[/teal]

[teal]dn: uid=Homer.Simpson, ou=People, o=simpson.com[/teal]
[teal]Department: BSW[/teal]
[teal]mailMessageStore: store8[/teal]

[teal]dn: uid=Maggie.Simpson, ou=People, o=simpson.com[/teal]
[teal]Department: CSW[/teal]
[teal]mailMessageStore: store8[/teal]

[teal]dn: uid=Marge.Simpson, ou=People, o=simpson.com[/teal]
[teal]Department: BSW[/teal]
[teal]mailMessageStore: store7[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Data::Dumper - stringified perl data structures, suitable for both printing and eval[/li]
[/ul]
[/tt]

- Miller
 
Thanks, I am a newbie so I hope you don't mind, are you running this and have the info in a file, for example, ./data.pl DATA, where data.pl is the script and DAT is the file. Thanks !!
 
Looks like the data structure will need some tweaking. This will overwrite records where $5 and $4 are repeated in the file:

$data{$5}{$4} = {uid => $1, ou => $2, o => 3};

dn: uid=Lisa.Simpson, ou=People, o=simpson.com
Department: CSW
mailMessageStore: store8

dn: uid=Homer.Simpson, ou=People, o=simpson.com
Department: BSW
mailMessageStore: store8

dn: uid=Maggie.Simpson, ou=People, o=simpson.com
Department: CSW
mailMessageStore: store8







------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I didn't post any code because I would first prefer to see what code the OP has written so far.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top