home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesa
- From: etxmesa@eos.ericsson.se (Michael Salmon)
- Subject: Re: problems with awk: output redirection and getline in a pipe
- Message-ID: <1992Nov16.093644.756@ericsson.se>
- Sender: news@ericsson.se
- Nntp-Posting-Host: eos6c02.ericsson.se
- Reply-To: etxmesa@eos.ericsson.se (Michael Salmon)
- Organization: Ericsson Telecom AB
- References: <34071@adm.brl.mil>
- Date: Mon, 16 Nov 1992 09:36:44 GMT
- Lines: 47
-
- In article <34071@adm.brl.mil>,
- Guenter.Partosch@hrz.uni-giessen.dbp.de writes:
- |> Subject: problems in awk: output redirection and getline in a pipe
- |>
- |> I have written the following shell script:
- |>
- |> awk 'BEGIN {print "...something"}
- |> {print " ...anything for every line"}
- |> END {printf("%2d\n", NR) >"awk.tmp" }' |
- |> sort |
- |> awk 'BEGIN {getline < "awk.tmp";
- |> numberoflines = $1}
- |> {print "---anything for every line"}
- |> END {printf("%2d\n", numberoflines)} '
- |>
- |> In this script awk is called and an input file is processed. Output
- |> is written into a pipe, temporary results are stored on a temporary file.
- |>
- |> The data in the pipe is transferred to UNIX sort and to a second awk program,
- |> which
- |> also reads the data on the temporary file by getline.
- |>
- |> If I start this script the first time I get 0 as result. If I start the
- |> script the next times I always get a number written on the temporary file
- |> in a previous run.
- |>
- |> What is to do now. Any help appreciated.
- |> Thanks in advance.
-
- When you start this command line you start up 3 processes
- simultaneously, awk, sort and, awk. awk.tmp is not created until the
- end of the first awk task. The second awk task reads from awk.tmp then
- proceeds to read input from the pipe where it halts until sort is
- finished. Solution: getline returns 0 if it reaches the end of its
- input file so, always remove awk.tmp before this command line and then
- loop waiting for getline to return something other than 0.
-
- --
-
- Michael Salmon
-
- #include <standard.disclaimer>
- #include <witty.saying>
- #include <fancy.pseudo.graphics>
-
- Ericsson Telecom AB
- Stockholm
-