On Sun Nov 09 2008 @  1:31, JC Janos wrote:
> I've read that Perl (which I don't know yet at all) is "best" for Text
> processing like this.
> 
> The thing is that I need to do this from within a Bash script, and
> assign the comma-separated list to a  variable in that Bash script.
> 
> Can I even use Perl like this, from inside a Bash script?

I can imagine two ways that you might do this, but neither one strikes me
as a good idea. First, you might create a Perl one-liner that could be run
just as is from within the shell script. Second, you could create a small,
self-contained Perl script, then assign the output from that to a variable
in the Bash script. Both alternatives seem error-prone and hackish to me.

More importantly, if Bash is a requirement and you don't know any Perl, I
can't quite see the value in adding Perl to your current problems.

That said, Perl could do what you need to do here pretty easily. In the
version below, I stuck the IP addresses into the same file (in the DATA
section) for convenience, but it wouldn't be any harder if they were in
a separate file. So maybe that's some incentive to learn Perl.

Hope this helps, T

#!/usr/bin/perl
use strict;
use warnings;

my @addresses;

while (<DATA>) {
  if ( /(!?\d+\.\d+\.\d+\.\d+(?:\/\d+)?)/ ) {
    push @addresses, $1;
  }
}

my $csv = join ",", @addresses;

print "$csv";
__DATA__
1.1.1.1      # comment A
2.2.2.2/29   # comment B
!3.3.3.3     # comment C
!4.4.4.4/28  # comment D

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to