Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Parsing output from a command

by sundialsvc4 (Abbot)
on Mar 24, 2009 at 17:54 UTC ( [id://752931]=note: print w/replies, xml ) Need Help??


in reply to Parsing output from a command

For completeness, let the record show that you can also choose not to use Perl at all, but rather awk.

awk is a more special-purpose tool, not intended to be a general-purpose programming language. But its “purpose” is very much what you are looking for here. (awk is one of the original “inspirations” for Perl and you'll see that its syntax is quite similar.)

An awk program generally consists of one or more declarations that look like this:

/pattern to look for/
{ block of code to execute anytime the pattern is found }

Notice that:   awk itself is the “main program.” It does all the work of opening the file, reading the records, separating them into “fields,” and searching for a matching pattern or patterns, which it does from top to bottom in their order of occurrence.

In addition to regular-expression patterns, certain keywords can be used. Any BEGIN block will be executed at the start of the file (for instance to establish the value of RS or record-separator). At the end of the file, the END block, if any, is run.

Within this general framework, any sort of text-file processing task can be very-simply handled, and you may (or, may not...) find it more expedient to do so than with Perl.

If you anticipate that the task might grow beyond what awk is designed to do, then you should probably select Perl from the outset. But for other tasks, IMHO, awk is “just the ticket.” In any case, I find it useful to know about this tool in addition to the Perl approaches given here, and I say you should definitely include it in your toolkit.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found