shellbusybox

How to iterate over grep -R output in busybox?


I would like to collect comments in a bunch of files which start with # into files which only contain the comments. I can do it this way:

find . -type f -exec grep '^#' {} > /path/to/comment/storage/{}

Very simple. This, however, is incredibly wasteful as it forks a grep for every file and creates a lot of empty files too. grep -R '^#' . would be much better but what do I do with the output of it? Something like export IFS=:;grep -R '^#' .|while read -r file contents; do echo $file-$contents; done is a promising start however this falls apart when the contents themselves also contain colons. It also has the challenge of properly creating and appending to each file.

As an additional restriction, this need to run with busybox as shell / awk / etc, GNU utils are not available.


Solution

  • As I write this it appears your input has no subdirectories, or that the ouput directory hierarchy has already been created. If that is the case, you can do:

    find . -type f -exec awk '
            FNR==1 {
                if (NR>FNR) close(out)
                out = "/path/to/comments/" FILENAME
            }
            /^#/ { print >out }
        ' {} +