From: miloody on
Dear all:
suppose i want to replace cat with dog in all the files within a
folder, how could I reach that with grep and sed?
i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
cannot eat the file names piped by grep.
How could I pass the file names to sed?
appreciate your help,
miloody
From: Janis Papanagnou on
miloody wrote:
> Dear all:
> suppose i want to replace cat with dog in all the files within a
> folder, how could I reach that with grep and sed?
> i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
> cannot eat the file names piped by grep.
> How could I pass the file names to sed?

Use xargs.

... | xargs sed ...


Janis

> appreciate your help,
> miloody
From: miloody on
On 11月22日, 下午3時13分, miloody <milo...(a)gmail.com> wrote:
> Dear all:
> suppose i want to replace cat with dog in all the files within a
> folder, how could I reach that with grep and sed?
> i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
> cannot eat the file names piped by grep.
> How could I pass the file names to sed?
> appreciate your help,
> miloody

Hi:
I write a shell script which first save the result of 'grep -lr 'cat'
*', and then use for loop feeding sed.
But why pipe command cannot work?
is there any rule about pipe, '|', command?
appreciate your help,
miloody
From: Chris F.A. Johnson on
On 2009-11-22, miloody wrote:
> On 11???22???, ??????3???13???, miloody <milo...(a)gmail.com> wrote:
>> Dear all:
>> suppose i want to replace cat with dog in all the files within a
>> folder, how could I reach that with grep and sed?
>> i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
>> cannot eat the file names piped by grep.
>> How could I pass the file names to sed?
>> appreciate your help,
>> miloody
>
> Hi:
> I write a shell script which first save the result of 'grep -lr 'cat'
> *', and then use for loop feeding sed.
> But why pipe command cannot work?
> is there any rule about pipe, '|', command?

A pipe sends its output to the input of the next command; sed
expects filenames on the command line, not in its standard input.

--
Chris F.A. Johnson, author <http://shell.cfajohnson,com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: Ben Finney on
miloody <miloody(a)gmail.com> writes:

> suppose i want to replace cat with dog in all the files within a
> folder, how could I reach that with grep and sed?

You'll need to get the file names to 'sed' on its own command line.

> i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
> cannot eat the file names piped by grep.

Right, because you're piping the output of a process 'grep …' to the
input of another process 'sed …'. But the 'sed' process is not looking
for file names on its standard input; it's expecting the file names on
its command line, and there are none there.

> How could I pass the file names to sed?

You can use 'xargs(1)' to read *its* standard input, and construct
command lines from the arguments that it reads.

Compare the output of this::

$ echo sed -ei 's/cat/dog/' foo bar baz

against the output of this::

$ grep -lr 'cat' * | xargs echo sed -ei 's/cat/dog/'

to understand what the role of 'xargs' is.

--
\ “The cost of education is trivial compared to the cost of |
`\ ignorance.” —Thomas Jefferson |
_o__) |
Ben Finney