Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Stop Reading Input Without EOF/nextfile/getline

Received: by 10.180.75.8 with SMTP id y8mr3220356wiv.4.1347409178870;
        Tue, 11 Sep 2012 17:19:38 -0700 (PDT)
Path: q11ni19625739wiw.1!nntp.google.com!feeder1.cambriumusenet.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!216.196.110.142.MISMATCH!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!border2.nntp.dca.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsgate.cuhk.edu.hk!goblin1!goblin.stu.neva.ru!news.space.net!news.m-online.net!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanag...@hotmail.com>
Newsgroups: comp.lang.awk
Subject: Re: Stop Reading Input Without EOF/nextfile/getline
Date: Wed, 05 Sep 2012 00:57:35 +0200
Organization: (posted via) M-net Telekommunikations GmbH
Lines: 80
Message-ID: <k260vs$qm2$1@news.m-online.net>
References: <201209042053432493@webuse.net>
NNTP-Posting-Host: ppp-88-217-66-157.dynamic.mnet-online.de
Mime-Version: 1.0
X-Trace: news.m-online.net 1346799420 27330 88.217.66.157 (4 Sep 2012 22:57:00 GMT)
X-Complaints-To: news@news.m-online.net
NNTP-Posting-Date: Tue, 4 Sep 2012 22:57:00 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0
In-Reply-To: <201209042053432493@webuse.net>
Bytes: 2960
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

On 04.09.2012 22:53, Ed Morton wrote:
> I was recently trying to answer a question and came across a problem I
> don't know if there is an answer to: how can you stop reading from an
> input stream without waiting for EOF or invoking getline or (in gawk)
> nextfile?
> 
> For example, let's say you have a script that prompts a user for some
> numeric value and then uses that value to multiply a set of other values
> stored in a file. e.g.:
> 
> $ cat file
> 4
> 5
> 2
> 
> $ awk -f mult.awk file        
> Enter multiplier: 3
> 3 * 4 = 12
> 3 * 5 = 15
> 3 * 2 = 6
> 
> $ awk -f mult.awk file
> Enter multiplier: 4
> 4 * 4 = 16
> 4 * 5 = 20
> 4 * 2 = 8
> 
> That could be implemented as either of these:
> 
> -------
> $ cat mult_getline.awk
> BEGIN {
>    printf "Enter multiplier: "
>    getline mult < "-"
> }
> 
> { printf "%d * %d = %d\n", mult, $0, mult * $0 }
> -------
> $ cat mult_nextfile.awk
> BEGIN {
>    ARGV[ARGC++] = ARGV[1]; ARGV[1] = "-"
>    printf "Enter multiplier: "
> }
> 
> NR==FNR {
>    mult = $0
>    nextfile
> }
> 
> { printf "%d * %d = %d\n", mult, $0, mult * $0 }
> -------
> 
> Is there any way to implement it without using getline or nextfile and
> without the user explicitly having to enter a control-D or anything else
> after their multiplier number, and without reading the whole data file
> into an array? The main point is to be able to stop reading the first
> input stream before an EOF is encountered.

Well, how should awk know about end of file? (Using close() doesn't seem
to work.)

[OT] You can force EOF at shell level...

  awk '
    BEGIN { printf "Enter multiplier: " }
    NR==FNR { mult = $0 ; next }
    { printf "%d * %d = %d\n", mult, $0, mult * $0 }
  ' <(head -1) file

No pure awk solution, but a concise and simple pattern at least.

Janis

> 
>    Ed.
> 
> 
> Posted using www.webuse.net 
>