Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

CLI formatting

by bahadur (Sexton)
on Jul 04, 2005 at 01:07 UTC ( [id://472101]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, i have just made a CLI and one of the commands give me this sort of an output.
nMetrics>Name.........Address..........Type..........PollInt.......... +Protocol Cisco2500.........192.168.1.254............SNMP............300.. +..........Interface 5 Cisco_Telnet.........192.168.1.254............Router............ +300............telnet Desktop_1.........192.168.1.5............NTServer............300...... +......icmp Desktop_2.........192.168.1.5............NTServer............300...... +......icmp Desktop_3.........192.168.1.5............NTServer............300...... +......icmp Desktop_4.........192.168.1.5............NTServer............300...... +......icmp Desktop_5.........192.168.1.5............NTServer............300...... +......icmp DevelopmentHost.........192.168.1.1............Router............300.. +..........icmp ExtMailServer.........192.168.1.101............MailServer............3 +00............smtp FileServer.........192.168.1.101............NTServer............300... +.........icmp FloorSwitch.........192.168.1.2............Switch/Hub............300.. +..........icmp MainSwitch.........192.168.1.12............Switch/Hub............300.. +..........89 PrintServer.........192.168.1.5............UnixHost............300.... +........icmp Swicth.........192.168.1.12............SNMP............300............ +Interface 12 test.........192.168.1.12............SNMP............300............In +terface 12 WebServer.........10.1.1.1............Router............300........... +.icmp
as u can see the output is poorly formatted. is there any module out there which can format this output for me in a nice way.

Replies are listed 'Best First'.
Re: CLI formatting
by sk (Curate) on Jul 04, 2005 at 01:52 UTC
    Did you write the code that generated this output? Are you wondering how to write the text out in a nice way? If so, then do  perldoc -f format and  perldoc perlform.

    This should give you a lot of information about how to format the text.

    I see you are trying to output fixed number of dots between fields..you want to assign a fixed length between fields and add dots based on that..

    If you are on Window$ you can also create a HTML table fairly quickly and then display it on the browser..

    If you are looking for something to capture that output you showed us and format it... check out what jpeg listed

    cheers

    SK

      @sk thanks for the help yes that output is from my code and i am trying to display it on the screen (command line) in a nice readable way much like the command top does on linux. i ll check the perl docs for that. if there is any other formating module avaiable do tell me.
Re: CLI formatting
by jpeg (Chaplain) on Jul 04, 2005 at 01:43 UTC
    There are probably modules, but backticks (or qx/ / to capture the output) and split(/\./, $line ) or a regex would do it. Check perldoc -f split, perldoc -f substr, and perldoc perlop for qx, and perldoc perlretut (sorry, but I don't post code until I see what people have tried.)

    When you say "I have just made a CLI" I guess you don't mean you wrote the original program? If you did or have access to it, why not fix it there?

    --
    jpg
Re: CLI formatting
by neniro (Priest) on Jul 04, 2005 at 08:27 UTC
    An example for using perls builtin format (see http://perldoc.perl.org/perlform.html for details):
    my ($name, $type, $len, $null, $pri, $inc); format STDOUT_TOP = Name ---------------- Type ------ Len--- Null Pri- Inc- . format STDOUT = @<<<<<<<<<<<<<<<<<<<| @<<<<<<<<<| @>>>>| @>>| @>>| @>>| $name, $type, $len, $null, $pri, $inc . for (@structure) { ($name, $type, $len, $null, $pri, $inc) = ( $_->{f_name}, $_->{f_type}, $_->{f_length}, $_->{f_nullable}, $_->{f_pri_key}, $_->{f_autoinc}, ); write (STDOUT); }
    It's easy to use, and it does all you want, so why do you search another module?
Re: CLI formatting
by jplindstrom (Monsignor) on Jul 04, 2005 at 09:22 UTC
    Text::Table is excellent for displaying tabular data in text mode.

    Highly recommended.

    /J

Log In?
Username:
Password:

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

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

    No recent polls found