Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to split file by two new lines (\n\n) and convert it into array

by kchavan (Initiate)
on Aug 20, 2014 at 18:52 UTC ( [id://1098142]=perlquestion: print w/replies, xml ) Need Help??

kchavan has asked for the wisdom of the Perl Monks concerning the following question:

how to split file by two new lines (\n\n) and convert it into array Input file example:
Status_EventId = "" Status_NmosSerial = "" Status_Acknowledged = 0 acknowledgementStatus = "notAck" additionalText = "" specificProblems00 = "" managedobjectInstance00 = "1.3.6.1.4.1.12.2.2.159#1/1.3.6.1.4.1.12.2.2 +.22#1/1.3.6.1.4.1.12.2.2.0#7863" perceivedSeverity = "warning" neLocationName = "" currentAlarmId = "8557984" lastOccurence = "1408338405832" acknowledgementUserName = "" additionalInfo00 = "RMServiceState =inService" additionalInfo01RMObjectType = "path" notificationType = "alarmRaise" remoteHost = "/10.23.13.153:25061" eventType = "communicationsAlarm" eventTime = "20140818050641" ASid = "3" firstOccurence = "1408338405832" probableCause = "Client Failure" reservationStatus = "notReserved" friendlyName = "B_GSM-R_N0a.12_N11BSC2.01" RawCaptureTimeStamp = 1408338405 Status_EventId = "" Status_NmosSerial = "" Status_Acknowledged = 0 currentAlarmId = "8557984" lastOccurence = "1408338448373" notificationType = "alarmClear" remoteHost = "/10.23.13.153:25061" eventTime = "20140818050723" ASid = "3" firstOccurence = "1408338448373" probableCause = "Client Failure" friendlyName = "B_GSM-R_N0a.12_N11BSC2.01" RawCaptureTimeStamp = 1408338448 Status_EventId = "" Status_NmosSerial = "" Status_Acknowledged = 0 acknowledgementStatus = "notAck" additionalText = "" specificProblems00 = "" managedobjectInstance00 = "1.3.6.1.4.1.12.2.2.159#1/1.3.6.1.4.1.12.2.2 +.22#1/1.3.6.1.4.1.12.2.2.69#711/1.3.6.1.4.1.12.2.2.70#13258/1.3.6.1.4 +.1.12.2.2.106#1057529" perceivedSeverity = "major" neLocationName = "" currentAlarmId = "8557985" lastOccurence = "1408338596741" acknowledgementUserName = "" additionalInfo00 = "RMServiceState =notInService" additionalInfo01RMObjectType = "lo-hotrail" notificationType = "alarmRaise" remoteHost = "/10.23.13.153:25061" eventType = "communicationsAlarm" eventTime = "20140818050646" ASid = "3" firstOccurence = "1408338596741" probableCause = "Transport Failure" reservationStatus = "notReserved" friendlyName = "Trail_S42.3_S42.6_01" RawCaptureTimeStamp = 1408338596
Output data format should be as below:
additionalInfo00,notificationType,eventType,friendlyName "RMServiceState =notInService","alarmRaise","communicationsAlarm","Tra +il_S42.3_S42.6_01"
The output file should has header and then data from the input file. I am not able to split file into array and assign vlaues to the keys in desired format. Kindly help me

Replies are listed 'Best First'.
Re: how to split file by two new lines (\n\n) and convert it into array
by NetWallah (Canon) on Aug 20, 2014 at 19:38 UTC
    use strict; use warnings; my $fname="file55.txt"; print "additionalInfo00,notificationType,eventType,friendlyName\n"; open my $f,"<",$fname or die "Cannot open $fname : $!"; local $/= "\n\n"; while (my $event =<$f>){ my %info; my @lines = split /\n/,$event; for (@lines){ chomp; next unless my ($k, $v)=/(\S+) = (.+)/; $v=~s/^\s*"//; $v=~s/"?\s*$//; $info{$k} = $v; } $info{additionalInfo00} or next; print "\"", join('","', map {$info{$_}} qw|additionalInfo00 notificationType e +ventType friendlyName|) ,"\"\n"; }
    Output:
    additionalInfo00,notificationType,eventType,friendlyName "RMServiceState =inService","alarmRaise","communicationsAlarm","B_GSM- +R_N0a.12_N11BSC2.01" "RMServiceState =notInService","alarmRaise","communicationsAlarm","Tra +il_S42.3_S42.6_01"

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: how to split file by two new lines (\n\n) and convert it into array
by Cristoforo (Curate) on Aug 20, 2014 at 19:19 UTC
    Please enclose your data with <code> your data </code> tags. It is unreadable now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1098142]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-23 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found