From: miloody on
Hi
> > 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.
>
Can I get the conclusion that pipe is the command that always send the
result to the standard input of the next command?
if so, how could I tell which command gets its input from standard
input or command line?

take grep for example:
grep -rw 'cat' * -----> grep get its input from cmd line
find ./ -name '*.c' --> find get its input from cmd line

are these surmises correct?
thanks for your reply,
miloody

From: Ben Finney on
miloody <miloody(a)gmail.com> writes:

> Can I get the conclusion that pipe is the command that always send the
> result to the standard input of the next command?

Pipe is an operation within a command; but otherwise, yes, that's right.
Read the Bash man page (if you're using Bash) for more; piping is one of
the “redirection” operators.

> if so, how could I tell which command gets its input from standard
> input or command line?

By reading the documentation for each command that you want to use.
(Notice that standard input and command line are often *both* used, for
different purposes, among other sources of input.)

--
\ “Perhaps our role on this planet is not to worship God — but to |
`\ create Him.” —Arthur C. Clarke, 1972 |
_o__) |
Ben Finney
From: Chris F.A. Johnson on
On 2009-11-22, miloody wrote:
> Hi
>> > 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.
>>
> Can I get the conclusion that pipe is the command that always send the
> result to the standard input of the next command?

A pipe connects the standard output of one command to the standard
input of another command.

> if so, how could I tell which command gets its input from standard
> input or command line?

A command gets its parameters from the command line; its input
usually comes from standard input or files specified as paramteres
on the command line.

> take grep for example:
> grep -rw 'cat' * -----> grep get its input from cmd line

It gets its input from the files specified by, in this case, *. If
no file is specified, it uses stdin as input.

> find ./ -name '*.c' --> find get its input from cmd line

Its input doesn't come from the command line; the command line
tells it where to get its input.

> are these surmises correct?

No.
--
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: bb on
On 2009-11-22 08:13, 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?
> appreciate your help,
> miloody

Try to run the part you have before the pipe first and see what output you get.
Then you can figure out if sed will find any cats in that list.

When I do things like that I use a perl oneliner like
perl -pi -e 's,cat,dog,g' *

but since you will do it recursive you can take your file list you created with
your grep.

perl -pi -e 's,cat,dog,g' $(grep -lr 'cat' *) unless it's to long list, or
includes file names with spaces.

/bb


From: Rakesh Sharma on
On Nov 22, 12:13 pm, 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


You could do this many ways:

find . \
\! -name . -prune
-type f \
-exec \
grep -li 'cat' {} \;
-exec \
sed -ie 's/cat/dog/g' {} \;

for _f in ./* ./.[!.]* ./..?*; do
if [ -f "$_f" ] && [ ! -L "$_f" ]; then
if grep -li 'cat' < $_f > /dev/null; then
sed -ie 's/cat/dog/g' "$_f"
fi
fi
done

perl -e '
for (<./* ./.[!.]* ./..?*>) {
system("perl -i.BAK -0777e '/cat/ && s//dog/g' \"$_\"") if ! -l
&& -f _;
}
'

--Rakesh