in reply to Re: Code Efficiency: Trial and Error?
in thread Code Efficiency: Trial and Error?

Update { should have benchmarked properly, apparently.. rest of this post largely invalidated by runrig's reply. }

Abigail-II's proposition of grep -l does not fit your specs as it will still scan all files, but your Perl can be simplified.

perl -e ' $site = shift; for $f (@ARGV) { local @ARGV = $f; /^Customer Code/ && last while <>; / \Q$site\E$/ && (print("$f\n"), last); } ' $code *
But why all that? A shortcircuiting sed script to find a customer's code: sed 's/^Customer Code[^ ] //; t done; d; : done; q;' FILE Wrap some sh around it and it does the job:
for ALIAS in * ; do [ "`sed 's/^Customer Code[^ ]* //; t done; d; : done; q' "$ALIAS"` +" = "$code" ] && break done echo "$ALIAS"
(Wrote this in bash, not sure if it works 1:1 in Korn, but it should be easy to port anyway.)

Update: Btw: when you're passing {print $?} to awk, it's a sign you really wanted to use cut - in your case, that would be cut -d: -f1

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^2: Code Efficiency: Trial and Error?
by runrig (Abbot) on Oct 14, 2002 at 20:23 UTC
    I didn't do the file globbing outside of the perl script because that seemed to be slower, and a readdir solution seemed to be faster than a glob inside of the script. I corrected and rewrote your shell solution for Korn:
    for ALIAS in * do [[ $(sed -n 's/^Customer Code.* // t done d : done p q' "$ALIAS") = $code ]] && break done print $ALIAS
    But this seems to be the slowest solution of all. Probably due to having to fire up a sed process so many times, and maybe also due to the specific regex. But like I say in my reply to Abigail-II's post above, I'll probably end up with grep -l just for the simplicity of it.

    Update: In addition to Aristotle's updated note's, another sign is that if you pipe grep to or from sed, awk, & maybe even cut, you are probably doing too much work, and may be better off just using one of the aforementioned commands instead.