Skip to content

Instantly share code, notes, and snippets.

Last active Jun 8, 2018
What would you like to do?
# 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!
$ 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
$ 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
$ cat
sed -n '/CAPTURE/,$p' test.txt | tac | sed -n '/CAPTURE/,$p' | tac
$ time ./ >/dev/null
real 0m48.955s
user 0m48.900s
sys 0m2.852s
$ 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
$ cat
echo "/CAPTURE/,?CAPTURE? p" | ed -s test.txt
$ time ./ >/dev/null
real 0m29.214s
user 0m25.156s
sys 0m2.628s
$ 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