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

Filterint information from external commands

by Dorficus (Initiate)
on Jan 25, 2012 at 13:16 UTC ( [id://949899]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm trying to display certain information of the disk in the termnial when they run my perl script. My problem is when I use:

 `parted /dev/sdb print`

I get all the information I need, but a little too much of it, I don't want to see what model it is, what size it is on the disk etc, any smart and easy ways to filter out this information?

Thanks in advance!

Replies are listed 'Best First'.
Re: Filterint information from external commands
by roboticus (Chancellor) on Jan 25, 2012 at 13:32 UTC

    Dorficus:

    Yes, using perl there are several ways that come to mind:

    • Use substr to clip out the bits of the lines you don't want,
    • Use split or unpack to break the lines apart, and then print only the columns you want,
    • Use regexes to extract only the bits you want, and then print that.

    Don't overlook the possibility that there may be command-line flags to the tool that may let you customize the output somewhat. Another option might be spelunking through the /proc filesystem (assuming you're on a Linux box) to get the information you want.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Filterint information from external commands
by reisinge (Hermit) on Jan 25, 2012 at 13:37 UTC
    One way would be using regular expressions to filter out lines you don't want:
    #!/usr/bin/perl use strict; use warnings; for (`parted /dev/sdb print`) { next if /^Model/; # Don't print model line print; }

    Have a nice day, j

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found