Re: A bit of grep and regex help?



Davorin Vlahovic wrote:

On 2007-02-16, Robert Newson <ReapNewsB@xxxxxxxxxxxxxxxxxxx> wrote:

Davorin Vlahovic wrote:

On 2007-02-15, Benny Nielsen <spam@xxxxxxxxxxx> wrote:

awk -F: '{if ($3 > 499) print $0}' < /etc/passwd

You don't need redirection. This works and is more efficient:

awk -F: '{if ($3 > 499) print $0}' /etc/passwd

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?

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].)

.



Relevant Pages

  • Re: Editable input from the console windows
    ... stdin, stdout, and stderror in relationship to pipe commands. ... I understand the fundamentals behind redirection. ... The exec command adds to the confusion as well. ... I learning the syntax and build upon a previously working script. ...
    (comp.unix.shell)
  • How to check if a thread waits for data at the read-end of a pipe?
    ... Win32 and in the Win32 version I need a way to check ... attach a semaphore to a named pipe and I can then use the other API ... My application acts as a GUI command line shell in this regard. ... child's STDIN, STDOUT and STDERR streams in standard documented ways. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to check if a thread waits for data at the read-end of a pipe?
    ... If thread in waiting mode you can use GetThreadContext ... > attach a semaphore to a named pipe and I can then use the other API ... > My application acts as a GUI command line shell in this regard. ... > child's STDIN, STDOUT and STDERR streams in standard documented ways. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to redirect stdout to stdin?
    ... stdout of nc to its stdin. ... If you could connect the output of a command to its input, ... Use a pair of pipes, at least one of which must be a named pipe ...
    (comp.unix.shell)
  • Re: edit and write multiple files with ed/ed/vi script
    ... IO redirection makes it into the ed clauses then it's time to consider ... A pipe specifies that the next command in line gets its ... is feeding it to the command in the pipe, ... Realistically you'll need to put the ed command in a script and have ...
    (comp.unix.admin)