Re: A bit of grep and regex help?
- From: Robert Newson <ReapNewsB@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 16 Feb 2007 20:29:31 GMT
Davorin Vlahovic wrote:
On 2007-02-16, Robert Newson <ReapNewsB@xxxxxxxxxxxxxxxxxxx> wrote:
Davorin Vlahovic wrote:
On 2007-02-15, Benny Nielsen <spam@xxxxxxxxxxx> wrote:Stupid question: why is [g]awk more efficient reading from a file specified on the command line as an argument as opposed to finding none there and using stdin?
awk -F: '{if ($3 > 499) print $0}' < /etc/passwdYou don't need redirection. This works and is more efficient:
awk -F: '{if ($3 > 499) print $0}' /etc/passwd
If you use redirection the shell opens the file and feeds the data into
another process.
And here I was thinking redirection was implemented via close and open (ie reopen), the shell process having nothing to do as the stdin of the command was connected /for it by the shell/ directly to the file, ie the shell roughly did:
if (child_of fork())
{
close(stdin)
open(file) as stdin
exec(new_command)
}
as opposed to the shell roughly doing:
if (child_of fork())
{
if (redirection)
{
make a pipe()
if (child_of fork())
{
connect write of pipe to stdout
exec(cat file)
}
connect read of pipe to stdin
}
exec(new_command)
}
If you use file as an argument to awk the system has no overhead
concerning routing the data between two processes.
I think you're thinking of:
$ cat file | command
which /requires/ two processes, a pipe for the data, is unefficient, is a useless use of cat and is *NOT* the same as:
$ command < file
which reopens stdin connected to the file [for the command]. (I think you'll find lsof agrees that a file redirected to stdin is connected directly to that file and not a pipe [from another process, eg the shell].)
.
- Follow-Ups:
- Re: A bit of grep and regex help?
- From: Davorin Vlahovic
- Re: A bit of grep and regex help?
- References:
- A bit of grep and regex help?
- From: Ivan Marsh
- Re: A bit of grep and regex help?
- From: Benny Nielsen
- Re: A bit of grep and regex help?
- From: Davorin Vlahovic
- Re: A bit of grep and regex help?
- From: Robert Newson
- Re: A bit of grep and regex help?
- From: Davorin Vlahovic
- A bit of grep and regex help?
- Prev by Date: Re: xorg.con / screen res help needed
- Next by Date: Re: xorg.con / screen res help needed
- Previous by thread: Re: A bit of grep and regex help?
- Next by thread: Re: A bit of grep and regex help?
- Index(es):
Relevant Pages
|