[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: format grep ouput



andy baxter wrote:

grep -R 'the' * | (of=""; while read f l ; do if [ "$f" != "$of" ] ; then echo ; fi; echo $f $l ; of=$f; done)

will put a linebreak after every new filename, as long the none of the filenames have spaces in.


This puzzled me for a while:

wrp@mercury:~/tmp$ cat input
dump_core.c:     * Set SA_RESETHAND so that the signal handler
dump_core.c:     * can re-raise the signal and get the default
wrp@mercury:~/tmp$ cat input | (of=""; while read f l ; do if [ "$f" != "$of" ] ; then echo ; fi; echo $f $l ; of=$f; done) | head

dump_core.c: a.c ac_example a.o a.out a.sh bar b.c core data.c data.h data.o difffile diffsum dir with spaces dump_core dump_core.c errors foo foo.c foo_f foo.o GNUmakefile grep.sh hello hello1.m hello1.o hello.m hello.o input insert.c libcircle.o Makefile obj objc output post qux shared_obj temp.csv tmp too t.sh your_code.c Set SA_RESETHAND so that the signal handler dump_core.c: a.c ac_example a.o a.out a.sh bar b.c core data.c data.h data.o difffile diffsum dir with spaces dump_core dump_core.c errors foo foo.c foo_f foo.o GNUmakefile grep.sh hello hello1.m hello1.o hello.m hello.o input insert.c libcircle.o Makefile obj objc output post qux shared_obj temp.csv tmp too t.sh your_code.c can re-raise the signal and get the default

Obviously, you also need to be careful if the output of grep contains
shell wildcards.

Does anyone know of a conenient way to escape them: eg:
$ grep -R 'the' * | escape_shell_wildcards | ...

That is, something other than:
$  sed 's/\([*|(\)]\)/\\\1/g'

Since (1) that's ugly as sin, and (2) I'm sure that doesn't
cover all the characters that need escaping.



Reply to: