Skip to main content
8 of 11
added 58 characters in body
jubilatious1
  • 3.9k
  • 10
  • 20

Using Raku (formerly known as Perl_6)

Code to remove lines 1000000 to 1000050 from the output of seq 1e7:

FASTEST EXAMPLE:

~$ seq 1e7 > seq_1e7.txt
~$ raku -e 'lines[0..999999].join("\n").put; lines.skip(50).join("\n").put'  < seq_1e7.txt 1<> seq_1e7_modified.txt

Timing (MacOS, M2 Max) typical run:

6.20s user 1.41s system 101% cpu 7.525 total

Raku doesn't have Perl's (or sed's) -i "in-place" command line flag, so the code above uses the <> Bourne/POSIX shell redirection operator as described by @StéphaneChazelas. Make backups always, and write to a tmp file whenever possible! See @StéphaneChazelas' excellent post for a precise explanation of this < file 1<> file code.


SLOWER EXAMPLES:

I expected the first codeblock below to be as fast as above, but it's about 3.2X slower (total time vs. total time):

~$ raku -e '.put for lines[0..999899]; .put for lines.skip(50)'  < seq_1e7.txt 1<> seq_1e7_modified.txt

The next codeblock below is an easy-to-read, but runs 3.2X-4.6X slower than the code at top (total time vs. total time):

~$ raku -ne '.put  unless  1000000 < ++$ < 1000050;' <  seq_1e7.txt  1<> seq_1e7_modified.txt

For the code immediately above, you can see how this Raku code is mash-up of @alexis' excellent Perl answer (translated to Raku), with file-operations again taken directly from @StéphaneChazelas' excellent post. Note how in Raku there are two fundamental changes from Perl:

  1. In Raku the $. variable is gone, replaced by ++$ anonymous state variable (used to count line-numbers).
  2. Raku can do "chained" inequalities such as 1000000 < ++$ < 1000050, obviating the need for the || short-circuiting operator as seen in the Perl code.

Finally, if you need to only remove a single line, here's the Raku translation of @abligh's Perl code. It runs about as fast as the codeblock immediately above:

~$ raku -ne '.put  unless  ++$ == 1000000;' <  seq_1e7.txt  1<> seq_1e7_modified.txt

https://en.wikipedia.org/wiki/Inequality_(mathematics)#Chained_notation
https://unix.stackexchange.com/a/66746/227738
https://raku.org

jubilatious1
  • 3.9k
  • 10
  • 20