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

Perl - convert file to CSV formatted file

Status
Not open for further replies.

santhas

Technical User
Aug 27, 2003
35
AU
I asked this question in Powershell forum but I didn't receive any help, so trying Perl now


I have file with data like this:

++++++++++++++++Input file +++++++++++++++++++++++++++++
User Name: Power DBA
Description:
Role: Power DBA
Last Login Date: Jul 1, 2011 4:54:18 PM

User Name: Power User
Description: Admin Team
Role: Power User
Last Login Date: Mar 15, 2011 8:16:19 AM

User Name: guest
Description: Monitoring
Role: Operator
Last Login Date: Dec 14, 2011 3:45:29 PM

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==



I need to convert this to CSV formatted file using Perl

+++++++++++++++++++++ file output should like this ++++++++++++++++++
User Name,Description, Role, Last Login Date

Power DBA,, Power DBA, Jul 1, 2011 4:54:18 PM

Power User,Admin Team,Power User, Mar 15, 2011 8:16:19 AM

guest,Monitoring,Operator,Dec 14, 2011 3:45:29 PM

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=====


Thanks

San
 
Hi

I let you to add the column headers :
Code:
perl -ne '[b]if[/b][teal]([/teal]$[teal].==[/teal][purple]1[/purple][teal])[/teal][teal]{[/teal][navy]@c[/navy][teal]=([/teal][green][i]"User Name"[/i][/green][teal],[/teal]Description[teal],[/teal]Role[teal],[/teal][green][i]"Last Login Date"[/i][/green][teal]);[/teal][b]print[/b] [b]join[/b][teal]([/teal][green][i]","[/i][/green][teal],[/teal][navy]@c[/navy][teal]),[/teal][green][i]"\n"[/i][/green][teal]}[/teal][b]if[/b][teal]([/teal][green][i]/(.+?): *(.*)/[/i][/green][teal])[/teal][teal]{[/teal][navy]$d[/navy][teal]{[/teal][navy]$1[/navy][teal]}[/teal][teal]=[/teal][navy]$2[/navy][teal]}[/teal][b]else[/b][teal]{[/teal][b]print[/b] [b]join[/b][teal]([/teal][green][i]","[/i][/green][teal],[/teal][navy]@d[/navy][teal]{[/teal][navy]@c[/navy][teal]}[/teal][teal]),[/teal][green][i]"\n"[/i][/green][teal]}[/teal]' /input/file

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top