sed and awk can also to this (1st line plus any matching lines)
Following transcript from zsh session on my fast Ryzen:
$ <<-'@@' time sh -c "grep -m1 USER && grep carenas"
USER,TIP
john,0
jane,10
carenas,100
@@
USER,TIP
carenas,100
sh -c "grep -m1 USER && grep carenas" 0.00s user 0.00s system
oops - grep slower than awk, not the other way around,
on these _highly_ inconclusive timings.
--
Paul Jackson
p...@usa.net
On the original test machine I timed the sed solution, as well as `(grep
-m1 . 'file' && grep 'pattern' 'file')` and `(mapfile -n1 <'file' && echo
$MAPFILE[0] && grep 'pattern' 'file')` and `(head -n1 'file' && grep
'pattern' 'file')`. Total table of speeds.
grep (v2.20):~1.15s
perl (
Ah - those times show another reason why one might
be motivated to keep requesting more options be added
to grep.
>From those timings, and from looking at the source, it's clear
that the FSF rewrote grep from scratch, sometime back in the
late 1980's or early 1990's, to have fast reads, whereas se
Re Perl's read speed, it's faster when not doing the line number check for
every line. So `perl -ne 'print if (/pattern/)'` is only ~2.60s, compared
to ~3.28s for `perl -ne 'print if ($. == 1 || /pattern/)'`. Doing nothing
in Perl, i.e., `perl -ne ''` is only ~1.38s.
Dan
On Wed, Aug 23, 2023 at 6