Using Raku (formerly known as Perl_6)
~$ raku -MText::CSV -e 'my @rows; my $csv = Text::CSV.new( sep => ","); \
while ($csv.getline($*IN)) -> $row { @rows.push: $row.map(*.trim) if 88000 < $row.[5] < 98000; }; \
.join(",").put for @rows;' < ~/raphui_771255.csv
Raku is a programming language in the Perl-family. It features high-level support for Unicode as well as a powerful Regex engine.
Above answer uses Raku's Text::CSV
module. The Perl(5) module Text::CSV_XS
is well-regarded, and a longtime author/maintainer of that module has gone on to develop Raku's Text::CSV
module (H. Merijn Brand, personal communication).
Sample Input (thanks to @aborruso!):
mcu_i,INIT,200,iFlash, 11593925, 88347,,0x00092684,r,0x4606b570, ok,, 32,single,op-c,0,, 0, 0, 0,
mcu_i,INIT,200,iFlash, 11593931, 88348,,0x00092678,r,0x28003801, ok,, 32,single,op-c,0,, 0, 0, 0,
mcu_i,INIT,200,iFlash, 10593931, 88348,,0x00092678,r,0x28003801, ok,, 32,single,op-c,0,, 0, 0, 0,
mcu_i,INIT,200,iFlash, 21593931, 98348,,0x00092678,r,0x28003801, ok,, 32,single,op-c,0,, 0, 0, 0,
mcu_i,INIT,200,iFlash, 31593931, 108348,,0x00092678,r,0x28003801, ok,, 32,single,op-c,0,, 0, 0, 0,
Sample Output:
mcu_i,INIT,200,iFlash,11593925,88347,,0x00092684,r,0x4606b570,ok,,32,single,op-c,0,,0,0,0,
mcu_i,INIT,200,iFlash,11593931,88348,,0x00092678,r,0x28003801,ok,,32,single,op-c,0,,0,0,0,
mcu_i,INIT,200,iFlash,10593931,88348,,0x00092678,r,0x28003801,ok,,32,single,op-c,0,,0,0,0,
Note: Raku allows the use of "chained" inequalities. Also, instead of hardcoding the values, Raku has a special associative array %*ENV
which can be used to access shell variables. So below takes shell variables startMarker
and stopMarker
from the environment (i.e. the shell). Use the high level csv( …, out => $*OUT)
function for output, and your whitespace-containing strings will automatically be quoted (in your case, drop the .trim
call as well):
~$ env startMarker="88000" stopMarker="89000" \
raku -MText::CSV -e 'my $start = %*ENV<startMarker>; my $stop = %*ENV<stopMarker>; \
my @rows; my $csv = Text::CSV.new( sep => ","); \
while ($csv.getline($*IN)) -> $row { @rows.push: $row if $start < $row.[5] < $stop; }; \
csv(in => @rows, out => $*OUT);' < ~/raphui_771255.csv
mcu_i,INIT,200,iFlash," 11593925"," 88347",,0x00092684,r,0x4606b570," ok",," 32",single,op-c,0,," 0"," 0"," 0",
mcu_i,INIT,200,iFlash," 11593931"," 88348",,0x00092678,r,0x28003801," ok",," 32",single,op-c,0,," 0"," 0"," 0",
mcu_i,INIT,200,iFlash," 10593931"," 88348",,0x00092678,r,0x28003801," ok",," 32",single,op-c,0,," 0"," 0"," 0",
https://raku.land/zef:Tux/Text::CSV
https://github.com/Tux/CSV/blob/master/doc/Text-CSV.md
https://docs.raku.org
https://raku.org