> $ 1} 'file' to single-line Perl? How can I convert: awk '{print $2 >> $1}' file in sho...">

What is the best way to convert "awk" {print $ 2 >> $ 1} 'file' to single-line Perl?

How can I convert:

awk '{print $2 >> $1}' file

in short single line perl?

A “file” might look like this:

fruit bananas
vegetable beets
vegetable carrots
mushrooms chanterelles
fruit apple

+3
source share
4 answers

Not pure Perl, but you can do:

perl -nae '`echo $F[1] >> $F[0]`' input_file
+2
source

there may be other ways, but here's what I can think of

perl -ane 'open(FILE,">>",$F[0]); print FILE $F[1];close(FILE);' file
+5
source

, awk : -)

, , .

$ perl -lane '$fh{$F[0]} || open $fh{$F[0]}, ">>", $F[0]; print {$fh{$F[0]}} $F[1]' file
+4

, a2p <<< '{print $2 >> $1}'

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches

$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

while (<>) {
    ($Fld1,$Fld2) = split(' ', $_, -1);
    &Pick('>>', $Fld1) &&
        (print $fh $Fld2);
}

sub Pick {
    local($mode,$name,$pipe) = @_;
    $fh = $name;
    open($name,$mode.$name.$pipe) unless $opened{$name}++;
}
+1

Source: https://habr.com/ru/post/1767727/


All Articles