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

I need some help. Please bear with me, my perl knowledge is very limited. I have a snippet from a text file. I need to combine the multiple lines that are between the timestamps into one line.
05/06/08 04:25:51 node2.mydomain.com not up for SNMP Deferring SNMP of ifOperStatus.89 on node2.mydomain.com 180 minutes (3.00 hours). Consecutive deferral #36 +. Run 'snmpcollect -C node1.mydomain.com' to retry sooner. 05/06/08 04:25:51 node3.mydomain.com not in topology database (Request +ed object is unknown), but trying anyway. Assuming node is up, SNMP is supported, sysObjectID and collection filter match. 05/06/08 04:25:51 node2.mydomain.com not in topology database (Request +ed object is unknown), but trying anyway. Assuming node is up, SNMP is supported, sysObjectID and collection filter match. 05/06/08 04:25:51 node4.mydomain.com not in topology database (Request +ed object is unknown), 05/06/08 05:07:53 node4.mydomain.com not in topology database (Request +ed object is unknown), but trying anyway. Assuming node is up, SNMP is supported, sysObjectID and collection filter match.
I would like it to look more like this...
05/06/08 04:25:51 node2.mydomain.com not up for SNMP Deferring SNMP of + ifOperStatus.89 on node2.mydomain.com 180 minutes (3.00 hours). Con +secutive deferral #36. Run 'snmpcollect -C node1.mydomain.com' to ret +ry sooner. 05/06/08 04:25:51 node3.mydomain.com not in topology database (Request +ed object is unknown),but trying anyway. Assuming node is up, SNMP i +s supported,sysObjectID and collection filter match. 05/06/08 04:25:51 node2.mydomain.com not in topology database (Request +ed object is unknown),but trying anyway. Assuming node is up, SNMP i +s supported,sysObjectID and collection filter match. 05/06/08 04:25:51 node4.mydomain.com not in topology database (Request +ed object is unknown), 05/06/08 05:07:53 node4.mydomain.com not in topology database (Request +ed object is unknown),but trying anyway. Assuming node is up, SNMP i +s supported,sysObjectID and collection filter match.
Any suggestions as to how I can do this?
Thank you very much
JB

Replies are listed 'Best First'.
Re: File manipulation
by pc88mxer (Vicar) on May 07, 2008 at 17:42 UTC
    Here's my old stand-by recipe for doing this:
    my $line; while (<>) { chomp; if (m{^\d\d/\d\d/\d\d \d\d:\d\d:\d\d }) { print $line, "\n" if $line; $line = ''; } $line .= (' ' . $_); } print $line, "\n" if $line;
Re: File manipulation
by apl (Monsignor) on May 07, 2008 at 18:30 UTC
    You're doing yourself a disservice by asking us how to implement this. What that says is that you don't know how to solve the problem. It's not a question of how to program this in Perl, it's a question of how to program.

    If you break the task into parts, you can then see what needs to be done.

    • Read a line.
    • Does the line start with a date/timestamp?
      • if yes, what do you need to do?
      • if no, what do you need to do?
    • Repeat.

    Ask the follow-up questions: when do you write out the line in the desired format? Do you need to do anything special when you've finished reading the input file?

    You can write the program to do this task. You just need to think about it.