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

Greetings, New here. Just learning Perl too. :-) I'm parsing through a test file. I need to parse through the text file until I find HW/OS/Client, print that line and then print each subsequent line of "clients" until the line read from the text file is blank. The text section in this very large text file looks something like this:
HW/OS/Client: PC Windows2003 server1
PC Windows2003 server2
PC Windows2003 server3
PC Windows2003 server4
PC Windows2000 server5
PC Windows2000 server6
(blank line here)
----more text in the file goes on from here----

In case you are wondering, I'm parsing through a Netbackup policy dump file and gathering up info on all the "Active" backup policies.

Thanks,
Dennis
  • Comment on How to print certain lines out of a text file.

Replies are listed 'Best First'.
Re: How to print certain lines out of a text file.
by Anonymous Monk on Jul 31, 2010 at 16:11 UTC
    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { next unless /^HW\/OS\/Client:/ .. /^$/; next if /^$/; print; } __DATA__ Greetings, New here. Just learning Perl too. :-) I'm parsing through a test file. I need to parse through the text file until I find HW/OS/Client print that line and then print each subsequent line of "clients" until the line read from the text file is blank. The text section in this very large text file looks HW/OS/Client: PC Windows2003 server1 PC Windows2003 server2 PC Windows2003 server3 PC Windows2003 server4 PC Windows2000 server5 PC Windows2000 server6 blah blah, blah blah blah, blah blah blah, blah blah blah, blah
      Thank you kindly. I'll give this a try in a bit. Sorry if it seems so "elementary", but some of this I'm trying to wrap my head around so I can understand better. Much appreciated.
        OK, Let's take a better look at what I truly meant to be more clear about.
        My text file contains backup policy information, many backup policies. Each section (policy) starts with "Policy Name:".
        I'm capturing that info and a few lines of data after "Policy Name:".
        Further down in the text file is:
        Policy Name: Backup_Monday Active: Yes Type: Normal ----- more various garbage in between ----- Granular Restore Info: No HW/OS/Client: PC Windows2003 ds1 PC Windows2003 ds2 PC Windows2003 ds3 PC Windows2003 ds4 PC Windows2000 ds5 PC Windows2000 ds6 (blank line here) (text file continues on to other policies in the file from here)
        I check the line in the file to see if it has the pattern in it (HW/OS/Client) and if it does, I want to print that line and then keep printing each subsequent line until the (blank line) is reached. Exit that loop and go back to the main loop to look for another iteration of "Policy Name:". Keep doing this through all 122 backup policies.

        I guess you'd say that I have a loop inside a loop.?

        Make Sense? or have I muddied the water even more?

        Thanks,
        Dennis
Re: How to print certain lines out of a text file.
by Anonymous Monk on Jul 31, 2010 at 15:03 UTC