in reply to splitting on multiple delimiters
#!/usr/bin/perl -l # use strict; use warnings; my $string = q{command1 command2 command3 "command4 --some-arg arg --some-other- +arg 2" command5}; my $inQuotes = 0; my $agregator = q{}; my @cmds = grep { defined } map { if ( $inQuotes ) { if ( m{"$} ) { s{"}{}; $agregator .= qq{ $_}; $inQuotes = 0; $agregator; } else { $agregator .= qq{ $_}; undef; } } elsif ( m{^"} ) { s{"}{}; $inQuotes = 1; $agregator = $_; undef; } else { $_; } } split m{\s+}, $string; print for @cmds;
The output.
command1 command2 command3 command4 --some-arg arg --some-other-arg 2 command5
I hope this is of interest.
Cheers,
JohnGG
|
|---|