CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [preston3271] How to convert "\n" into CSV comma delimited Output: Edit Log



Kenosis
User

May 16, 2013, 11:52 PM


Views: 401
Re: [preston3271] How to convert "\n" into CSV comma delimited Output

Here's another option that also operates on equal-sign-delimited records:

Code
use strict; 
use warnings;
use v5.10;

local $/ = '=' x 68;
while (<DATA>) {
chomp;
print +( join ',', map s/^\s+|\s+$//gr, /=(.+)/g ) . "\n" if /\S/;
}

__DATA__
====================================================================
Name = stlc7c02
Label =
IP-Address = 10.213.0.97
Network Type = NETWORK_IP
Machine Type = MACH_OTHER
Comm Type = COMM_BBC
DHCP enabled = no (0x22)
====================================================================
Name = isntcomnsortb
Label =
IP-Address = 10.212.62.58
Network Type = NETWORK_IP
Machine Type = MACH_OTHER
Comm Type = COMM_BBC
DHCP enabled = no (0x22)
====================================================================

Output:

Code
stlc7c02,,10.213.0.97,NETWORK_IP,MACH_OTHER,COMM_BBC,no (0x22) 
isntcomnsortb,,10.212.62.58,NETWORK_IP,MACH_OTHER,COMM_BBC,no (0x22)

The regex first captures all after each equals sign, then a substitution is used within map to remove leading/trailing spaces and return the string. Finally, the strings are joined and printed.

Hope this helps!


(This post was edited by Kenosis on May 16, 2013, 11:55 PM)


Edit Log:
Post edited by Kenosis (User) on May 16, 2013, 11:54 PM
Post edited by Kenosis (User) on May 16, 2013, 11:54 PM
Post edited by Kenosis (User) on May 16, 2013, 11:55 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives