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

Hi all, I'm trying to format the output of lsmod for inclusion in a phpBB forum so that the lines get wrapped nicely at a certain length and you don't have to scroll horizontally forever to hit the reply button. And I'd like to do the same with the apache error log. Starting with the output of lsmod I tried different approaches garnered on the net none of which produced the expected result. Something I tried:
#!/usr/bin/perl use Text::Wrap; $Text::Wrap::columns = 100; @modules = `lsmod`; print Text::Wrap::fill( '', '', @modules );
It produces this output:
ip_tables 9688 3 iptable_mangle,iptable_nat,iptable_filter x_tables 8964 17 xt_CONNMARK,xt_length,xt_tcpudp,ipt_dscp,ipt_ipp2p,xt_connmark,ipt_mul +tiport,xt_state,ipt_TOS,xt_CL ASSIFY,ipt_ACCOUNT,ipt_MASQUERADE,ipt_REDIRECT,ipt_REJECT,ipt_LOG,ipta +ble_nat,ip_tables sch_htb 14464 0 sch_sfq 4992 0 ppp_async 8192 0 crc_ccitt 2048 1 ppp_async ppp_synctty 6912 0 ppp_generic 21140 2 ppp_async,ppp_synctty
It's the closest I've come to my goal but you see there are extraneous carriage returns as compared to the lsmod output:
ip_tables 9688 3 iptable_mangle,iptable_nat,iptable_fil +ter x_tables 8964 17 xt_CONNMARK,xt_length,xt_tcpudp,ipt_d +scp,ipt_ipp2p,xt_connmark,ipt_multiport,xt_state,ipt_TOS,xt_CLASSIFY, +ipt_ACCOUNT,ipt_MASQUERADE,ipt_REDIRECT,ipt_REJECT,ipt_LOG,iptable_na +t,ip_tables sch_htb 14464 0 sch_sfq 4992 0 ppp_async 8192 0 crc_ccitt 2048 1 ppp_async ppp_synctty 6912 0 ppp_generic 21140 2 ppp_async,ppp_synctty
And also you lose the original formatting by neat columns. Is there a solution to my problem ?
TIA
Cheers, Pascal

Replies are listed 'Best First'.
Re: Formatting text, eg long lines
by wind (Priest) on Nov 30, 2007 at 10:08 UTC
    It looks like you could use a simple regular expression. Try the following:
    use strict; my $data = do {local $/; <DATA>}; # Will wrap lines longer then 50 characters $data =~ s{(.{50})(?=.)}{$1\n}g; print $data; __DATA__ ip_tables 9688 3 iptable_mangle,iptable_nat,iptable_fil +ter x_tables 8964 17 xt_CONNMARK,xt_length,xt_tcpudp,ipt_d +scp,ipt_ipp2p,xt_connmark,ipt_multiport,xt_state,ipt_TOS,xt_CLASSIFY, +ipt_ACCOUNT,ipt_MASQUERADE,ipt_REDIRECT,ipt_REJECT,ipt_LOG,iptable_na +t,ip_tables sch_htb 14464 0 sch_sfq 4992 0 ppp_async 8192 0 crc_ccitt 2048 1 ppp_async ppp_synctty 6912 0 ppp_generic 21140 2 ppp_async,ppp_synctty
    - Miller
      Works a treat! Thanks a bunch.
      Cheers
      Pascal
      P.S. : would you please comment your code so that I could learn something?
Re: Formatting text, eg long lines
by Cristoforo (Curate) on Nov 30, 2007 at 20:17 UTC
    You should post the first lines of your program as:
    #!/usr/bin/perl use strict; use warnings;
    as they provide help to find errors in your code.

    Maybe chomp: chomp(my @modules = `lsmod`); will eliminate the extra newlines.

    The module Perl6::Form may be of help here although I've not used it, and there's a bit of documentation and I haven't read it all.

    I tried something like this

    #!/usr/bin/perl use strict; use warnings; use Perl6::Form; my @modules = <DATA>; for (@modules) { my ($tables, $count, $count2, $text) = split; print form "{<<<<<<<<<<} {>>>>>} {>>} {[[[[[[[[[[[[[[[[[[[[[[[[[[ +[[[[[[}", $tables, $count, $count2, $text; } __DATA__ ip_tables 9688 3 iptable_mangle,iptable_nat,iptable_fil +ter x_tables 8964 17 xt_CONNMARK,xt_length,xt_tcpudp,ipt_d +scp,ipt_ipp2p,xt_connmark,ipt_multiport,xt_state,ipt_TOS,xt_CLASSIFY, +ipt_ACCOUNT,ipt_MASQUERADE,ipt_REDIRECT,ipt_REJECT,ipt_LOG,iptable_na +t,ip_tables sch_htb 14464 0 sch_sfq 4992 0 ppp_async 8192 0 crc_ccitt 2048 1 ppp_async ppp_synctty 6912 0 ppp_generic 21140 2 ppp_async,ppp_synctty
    Output is
    C:\perlp>perl t2.pl ip_tables 9688 3 iptable_mangle,iptable_nat,iptabl- e_filter x_tables 8964 17 xt_CONNMARK,xt_length,xt_tcpudp,i- pt_dscp,ipt_ipp2p,xt_connmark,ipt- _multiport,xt_state,ipt_TOS,xt_CL- ASSIFY,ipt_ACCOUNT,ipt_MASQUERADE- ,ipt_REDIRECT,ipt_REJECT,ipt_LOG,- iptable_nat,ip_tables sch_htb 14464 0 sch_sfq 4992 0 ppp_async 8192 0 crc_ccitt 2048 1 ppp_async ppp_synctty 6912 0 ppp_generic 21140 2 ppp_async,ppp_synctty