Ambiguous redirect, grep command

It was an exercise from one Linux textbook: to give an example both input and output redirected. I found an answer in web:

grep \$Id < *.c > id_list

But this *.c seems to be the problem. Can this answer be worked around?

3

2 Answers

Since grep can read from files whose names are passed to it as arguments, you don't need input redirection at all here:

grep '$Id' -- *.c > id_list

By default, if *.c expands to a list of more than one file, then grep will prefix each match output with the filename in which it was found; if you don't want that, add the -h (or --no-filename) switch.

For a recursive search, you can make use of the --include option

grep -r --include='*.c' '$Id' . > id_list

It's not possible to redirect more than of one file to input using <. ( or even output, for output you should use tee).

To demonstrate a working example, you can use something like: grep \$Id < 1.c > id_list.

You can also use "here string" type of redirection to get you want.

grep <<< $(cat *.c) > out
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like