Skip to content

Instantly share code, notes, and snippets.

@haukex
Last active June 8, 2018 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haukex/36347160abb45e117182f0305a8c27ae to your computer and use it in GitHub Desktop.
Save haukex/36347160abb45e117182f0305a8c27ae to your computer and use it in GitHub Desktop.
# WARNING # WARNING # WARNING # the following command generates a ~1 GIGABYTE file!
$ perl -le '$,=$/;for(1..50000){print+("Foo")x5000,"Bar CAPTURE Bar"}print+("Foo")x5000' >test.txt
# For testing outputs:
# perl -le '$,=$/;print"Bar CAPTURE Bar";for(1..49999){print+("Foo")x5000,"Bar CAPTURE Bar"}' >out_expect.txt
# UPDATE: Replace "time" with "/usr/bin/time" to get memory usage stats as well!
# https://stackoverflow.com/a/50728937
$ time perl -ne '$x&&push@b,$_;if(/CAPTURE/){$x||=@b=$_;print@b;@b=()}' test.txt >/dev/null
real 0m50.761s
user 0m50.560s
sys 0m0.196s
# https://stackoverflow.com/a/50728937
$ time perl -0777ne '/((^.*CAPTURE.*$)(?s:.*)(?2)(?:\z|\n))/m and print $1' test.txt >/dev/null
real 0m0.579s
user 0m0.328s
sys 0m0.248s
# https://stackoverflow.com/a/50728589
$ cat glenn.sh
#!/bin/sh
sed -n '/CAPTURE/,$p' test.txt | tac | sed -n '/CAPTURE/,$p' | tac
$ time ./glenn.sh >/dev/null
real 0m48.955s
user 0m48.900s
sys 0m2.852s
# https://stackoverflow.com/a/50728348
$ time awk '/CAPTURE/ && NR==FNR {a[++f]=NR; next} a[1]<=FNR && FNR<=a[f]' test.txt{,} >/dev/null
real 1m36.760s
user 1m36.476s
sys 0m0.276s
# https://stackoverflow.com/a/50729789
$ cat walter1.sh
#!/bin/sh
echo "/CAPTURE/,?CAPTURE? p" | ed -s test.txt
$ time ./walter1.sh >/dev/null
real 0m29.214s
user 0m25.156s
sys 0m2.628s
# https://stackoverflow.com/a/50729789
$ time sed -n '1,/CAPTURE/{h;n};H;/CAPTURE/{x;p;n;h};' test.txt >/dev/null
real 0m29.852s
user 0m29.676s
sys 0m0.172s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment