Skip to main content
added 283 characters in body
Source Link
jubilatious1
  • 3.9k
  • 10
  • 20

Below, using run "printf", "%s\t", .[0].uc, .[1], .[2].uc given $_; run "printf", "\n";, separating column output with \t tabs. Note here we add .uc to uppercase the first and third columns, to show that you can still clean up text if you need to (before invoking your my_cmd):

Finally, you can take input files off the command line using Raku's $*ARGFILES dynamic variable. Obviously, you'll substitute your my_cmd in place of printf below:

~$ raku -MText::CSV -e '
         my $parser = Text::CSV.new;    
         until $*ARGFILES.eof {
               $_ = $parser.getline($*ARGFILES); 
               run "printf", "%s\t""%s ", "--arg1", .[0].uc, "--arg2", .[1], "--arg3", .[2].uc given $_; 
               run "printf", "\n";
         };'   luator.txt
FOO--arg1 foo --arg2 42 --arg3 REDred
BAR--arg1 bar --arg2 13 --arg3 BLUEblue
BAZ--arg1 baz --arg2 27 --arg3 GREENgreen
 

SeeOtherwise, see the first link below for how to save output to a Raku "Proc" (process) object, or the second link below for using "Proc::Async" (asynchronous process interface).

Below, using run "printf", "%s\t", .[0].uc, .[1], .[2].uc given $_; run "printf", "\n";. Note here we add .uc to uppercase the first and third columns:

Finally, you can take files off the command line using Raku's $*ARGFILES dynamic variable:

~$ raku -MText::CSV -e '
         my $parser = Text::CSV.new;    
         until $*ARGFILES.eof {
               $_ = $parser.getline($*ARGFILES); 
               run "printf", "%s\t", .[0].uc, .[1], .[2].uc given $_; 
               run "printf", "\n";
         };' luator.txt
FOO 42  RED
BAR 13  BLUE
BAZ 27  GREEN

See the first link below for how to save output to a Raku "Proc" (process) object, or the second link below for using "Proc::Async" (asynchronous process interface).

Below, using run "printf", "%s\t", .[0].uc, .[1], .[2].uc given $_; run "printf", "\n";, separating column output with \t tabs. Note here we add .uc to uppercase the first and third columns, to show that you can still clean up text if you need to (before invoking your my_cmd):

Finally, you can take input files off the command line using Raku's $*ARGFILES dynamic variable. Obviously, you'll substitute your my_cmd in place of printf below:

~$ raku -MText::CSV -e '
         my $parser = Text::CSV.new;    
         until $*ARGFILES.eof {
               $_ = $parser.getline($*ARGFILES); 
         run "printf", "%s ", "--arg1", .[0], "--arg2", .[1], "--arg3", .[2] given $_;
         run "printf", "\n";
   };'   luator.txt
--arg1 foo --arg2 42 --arg3 red
--arg1 bar --arg2 13 --arg3 blue
--arg1 baz --arg2 27 --arg3 green
 

Otherwise, see the first link below for how to save output to a Raku "Proc" (process) object, or the second link below for using "Proc::Async" (asynchronous process interface).

Source Link
jubilatious1
  • 3.9k
  • 10
  • 20

Using Raku (formerly known as Perl_6)

...with Raku's Text::CSV module:

~$  raku -MText::CSV -e '

    my $fh = open "luator.txt", :r;
    my $parser = Text::CSV.new;

    until $fh.eof {
          $_ = $parser.getline($fh); 
          run "echo", .[0], .[1], .[2] given $_;
    }

    $fh.close;'

Raku is a programming language in the Perl-family that features some nice functions for invoking external commands. The two options are calling shell or calling run. According to the Docs, calling run is safer.

Above, when you declare the $parser object you can set various parameters, such as accepting a non-comma separator (example: my $parser = Text::CSV.new(sep => "|");). Then the file is read/parsed linewise with getline(). A simple output is shown above using echo.

Sample Input:

~$ cat luator.txt
foo,42,red
bar,13,blue
baz,27,green

Sample Output (with echo):

foo 42 red
bar 13 blue
baz 27 green

Below, using run "printf", "%s\t", .[0].uc, .[1], .[2].uc given $_; run "printf", "\n";. Note here we add .uc to uppercase the first and third columns:

Sample Output (with printf):

FOO 42  RED
BAR 13  BLUE
BAZ 27  GREEN

Finally, you can take files off the command line using Raku's $*ARGFILES dynamic variable:

~$ raku -MText::CSV -e '
         my $parser = Text::CSV.new;    
         until $*ARGFILES.eof {
               $_ = $parser.getline($*ARGFILES); 
               run "printf", "%s\t", .[0].uc, .[1], .[2].uc given $_; 
               run "printf", "\n";
         };' luator.txt
FOO 42  RED
BAR 13  BLUE
BAZ 27  GREEN

See the first link below for how to save output to a Raku "Proc" (process) object, or the second link below for using "Proc::Async" (asynchronous process interface).

https://docs.raku.org/type/Proc
https://docs.raku.org/type/Proc/Async
https://raku.org