On 10/24/2011 12:14 PM, Eric Blake wrote:
find -type f -name bp_fs.h \
-exec sed -n '1,5{/\$Header:/q1};6q' {} \; -print

Alas, I don't know of any way to use -exec ... {} + for fewer processes,
even with sed.git with the -s option and the new F command, because I
don't have enough sed expertise to write a script that outputs the
filename of each of its multiple input files exactly in the case where
the first five lines do not contain a regex match.

Aha, I realized how to do it, using the 'N' operator. This spawns as few processes as possible, while still giving the same answer (assuming, of course, that you are using sed.git or the not-yet-released sed 4.2.2):

find -type f -name bp_fs.h \
 -exec sed -sn '1{N;N;N;N;/$Header:/bl;F;:l}' {} +

It's still not as efficient as I'd like (I don't know any way to make sed avoid reading the rest of the file, and just skip to the next, once it has finished its analysis of the first 5 lines). But efficiency within sed is much less of a problem than spawning one sed process per file.

--
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Reply via email to