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

hi all, Currently, I am doing HP NNM to capture device SNMP track information. The value that passing in is $1, $2, $3 etc. Discovered that $3 value is a multiple lines of words which cannot be accept by HP NNM. Hence, how should I write a script to combine the multiple lines into 1 line. Hope to hear from you soon. Million Thanks. rgds, hyliau

Replies are listed 'Best First'.
Re: Combine multiple lines into one line
by gellyfish (Monsignor) on Jun 22, 2003 at 10:50 UTC

    Firstly, you really should avoid using $1, $2 etc for your own variables in your code as these are used by Perl for capture in regex and the use of them for other purposes can lead to weird side effects.

    Removing line separators from a string is simple - you can do something like:

    my $foo =<<EOF; line 2 line 3 line 4 EOF $foo =~ s/\r?\n/ /g; print $foo;
    You probably want to see the perlre manpage if you are not certain about regular expressions.

    /J\
    
Re: Combine multiple lines into one line
by Zaxo (Archbishop) on Jun 22, 2003 at 13:20 UTC

    You can convert newlines to spaces with: $var =~ tr/\n/ /; or, maybe better: $var = join ' ', split ' ', $var; That split is the magical one that trims all whitespace.

    After Compline,
    Zaxo

      hi Zaxo, Does your suggested syntax can be used in Shell script? or is a pure perl script. Can you guide me on how to test this? Maybe a complete script if possible. Hope to hear you soon. Thanks. rgds, hyliau
Re: Combine multiple lines into one line
by Skeeve (Parson) on Jun 23, 2003 at 07:07 UTC
    Here is a "complete" perl script that will remove any linefeeds and formfeeds in your parameters.
    #!/path/to/your/perl # $1 $2 $3 (shell) are in $ARGV[0], $ARGV[1] and $ARGV[2] (perl) $ARGV[2]=~ s/[\r\n]+/ /g; # Now $3/$ARGV[2] doesn't contain any newline/linefeed anymore $shellindex=1; foreach (@ARGV) { print '$',$shellindex,' ($ARGV[',$shellindex-1,']: ',$_,"\n"; ++$shellindex; }
      hi Skeeve, Can this be done in Shell script? How? Btw, I would like to test this. If I create a text file which contain multiple lines, how do I pass this file into this script and test the output? One more thing, if the multiple lines contain character like double-quote ("), and I would like to get rid of those wild character, how should I do it? Appreciate your help. Thanks. rgds, hyliau
        hi, Anybody can help me on the last posted issued by me. That is, how to get rid of wild character like double-quote (") in a multiple line? Anywhere to change the Perl script to Shell script as well? Thanks. rgds, hyliau