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:
How to convert "\n" into CSV comma delimited Output

 



preston3271
Novice

May 14, 2013, 2:54 PM

Post #1 of 8 (439 views)
How to convert "\n" into CSV comma delimited Output Can't Post

Hello, Perl Believers:

How can I convert a sample which is seen below


Code
==================================================================== 
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)
====================================================================


into something like

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


My perl fu (fu = hard work) fails me as I don't know where to begin with stripping out the ===== and removing the newline (\n). I need the help of a Perl Guru!


Laurent_R
Veteran / Moderator

May 14, 2013, 3:10 PM

Post #2 of 8 (436 views)
Re: [preston3271] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post

Perhaps something along these lines:


Code
my @list; 
while (<$INPUT>) {
next if /===/;
chomp;
my $value = (split /=/, $_)[1];
push @list, $value;
}
print join ",", @list;



preston3271
Novice

May 14, 2013, 3:15 PM

Post #3 of 8 (435 views)
Re: [Laurent_R] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post

Laurent that was too fast! My "Fu" is in question now. Can you explain line 5

Code
my $value = (split /=/, $_)[1];


I understand the split /<find pattern>/, and $_ whatever held by but I haven't seen [1] used in this matter.


preston3271
Novice

May 14, 2013, 4:01 PM

Post #4 of 8 (434 views)
Re: [preston3271] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post

That was right on the money. Question, how I make a newline after the last value (0x22)?


preston3271
Novice

May 14, 2013, 4:48 PM

Post #5 of 8 (428 views)
Re: [preston3271] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post

I have came up with :


Code
#!/usr/bin/perl 
####################################

`/opt/OV/bin/OpC/utils/opcnode -list_nodes > /export/home/lthornto/nodelist.txt`;
$filelist = '/export/home/lthornto/nodelist.txt';
open(FH, $filelist) || die "Can't open nodelist.txt: $!\n";
my @list;
while (<FH>) {
next if /===/;
s/\s+\w+\s+\(0x22\)/\n/;
chomp;
my $value = (split /=/, $_)[1];
push @list, $value;
}
print join ",", @list;

Output

, swftcomnsorta, , 10.x.62.7, NETWORK_IP, MACH_OTHER, COMM_BBC,
, swftcomnsopm1, , 10.x.10.20, NETWORK_IP, MACH_OTHER, COMM_BBC,
, rmsycomnecpm2, , 10.x.11.22, NETWORK_IP, MACH_OTHER, COMM_BBC,


How can I clean up the lead comma if possible?


BillKSmith
Veteran

May 14, 2013, 5:16 PM

Post #6 of 8 (424 views)
Re: [preston3271] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post

I find it clearer to work with logical records.

Code
use strict; 
use warnings;
$/ = "====================================================================\n";
<DATA>;
while (my $inrecord = <DATA>) {
chomp $inrecord;
my @outrecord = split /\n/, $inrecord;
@outrecord = map {m/\=\s*(.*)\s*$/; $1} @outrecord;
$" = ',';
print "@outrecord\n";
}

__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)
====================================================================

Good Luck,
Bill


Laurent_R
Veteran / Moderator

May 14, 2013, 11:19 PM

Post #7 of 8 (419 views)
Re: [preston3271] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post


In Reply To
Laurent that was too fast! My "Fu" is in question now. Can you explain line 5

Code
my $value = (split /=/, $_)[1];


I understand the split /<find pattern>/, and $_ whatever held by but I haven't seen [1] used in this matter.


[1] just retains only the second field returned by split.


Kenosis
User

May 16, 2013, 11:52 PM

Post #8 of 8 (407 views)
Re: [preston3271] How to convert "\n" into CSV comma delimited Output [In reply to] Can't Post

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)

 
 


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

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