home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13468 < prev    next >
Encoding:
Text File  |  1992-11-15  |  2.1 KB  |  61 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!mcsun!sunic!ericom!eos.ericsson.se!etxmesa
  3. From: etxmesa@eos.ericsson.se (Michael Salmon)
  4. Subject: Re: problems with awk: output redirection and getline in a pipe
  5. Message-ID: <1992Nov16.093644.756@ericsson.se>
  6. Sender: news@ericsson.se
  7. Nntp-Posting-Host: eos6c02.ericsson.se
  8. Reply-To: etxmesa@eos.ericsson.se (Michael Salmon)
  9. Organization: Ericsson Telecom AB
  10. References:  <34071@adm.brl.mil>
  11. Date: Mon, 16 Nov 1992 09:36:44 GMT
  12. Lines: 47
  13.  
  14. In article <34071@adm.brl.mil>, 
  15. Guenter.Partosch@hrz.uni-giessen.dbp.de writes:
  16. |> Subject: problems in awk: output redirection and getline in a pipe
  17. |> 
  18. |> I have written the following shell script:
  19. |> 
  20. |> awk 'BEGIN {print "...something"}
  21. |>            {print " ...anything for every line"}
  22. |>     END    {printf("%2d\n", NR) >"awk.tmp" }' |
  23. |> sort |
  24. |> awk 'BEGIN {getline < "awk.tmp";
  25. |>            numberoflines = $1}
  26. |>            {print "---anything for every line"}
  27. |>      END   {printf("%2d\n", numberoflines)} '
  28. |> 
  29. |> In this script awk is called and an input file is processed. Output
  30. |> is written into a pipe, temporary results are stored on a temporary file.
  31. |> 
  32. |> The data in the pipe is transferred to UNIX sort and to a second awk program,
  33. |> which
  34. |> also reads the data on the temporary file by getline.
  35. |> 
  36. |> If I start this script the first time I get 0 as result. If I start the
  37. |> script the next times I always get a number written on the temporary file
  38. |> in a previous run.
  39. |> 
  40. |> What is to do now. Any help appreciated.
  41. |> Thanks in advance.
  42.  
  43. When you start this command line you start up 3 processes
  44. simultaneously, awk, sort and, awk. awk.tmp is not created until the
  45. end of the first awk task. The second awk task reads from awk.tmp then
  46. proceeds to read input from the pipe where it halts until sort is
  47. finished. Solution: getline returns 0 if it reaches the end of its
  48. input file so, always remove awk.tmp before this command line and then
  49. loop waiting for getline to return something other than 0.
  50.  
  51. -- 
  52.  
  53. Michael Salmon
  54.  
  55. #include    <standard.disclaimer>
  56. #include    <witty.saying>
  57. #include    <fancy.pseudo.graphics>
  58.  
  59. Ericsson Telecom AB
  60. Stockholm
  61.