help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use variable from BEGIN as input to remaining program


From: Neil R. Ormos
Subject: Re: Use variable from BEGIN as input to remaining program
Date: Wed, 6 Sep 2023 13:37:28 -0500 (CDT)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)

port19 wrote:

> I have now managed to send https requests via the use of stunnel.
> My current problem is that it seems impossible to reuse the response.
> In my limited research I did not manage to find ways to define records in 
> BEGIN.

> Is it possible to use output retrieved in BEGIN as input for the rest of the 
> program?
> If yes, how so?
> If not, how would you proceed?

> Context is the rewrite of a small reddit client. [1]

> [1] https://github.com/port19x/redqu/tree/gawk

Are you trying to "reuse the response" by, in effect, injecting the "output 
retrieved in BEGIN" as records to be processed by other, conventional 
pattern--action rules in the same awk program, in the same manner as input 
records received from STDIN or a file mentioned on the command line would be 
processed?

I'm not aware of a direct way to do to that.  Trivially, you could achieve 
something similar by sending the results to another invocation of awk.  I'm 
sure some would criticize that as ugly and inefficient.  There might also be 
some clever use of pipes, coprocesses, or even temporary files, that would work.

However, if you are trying to process records using awk's typical FS-style 
field splitting and don't need range-expression patterns, it would be most 
straightforward simply to process the response you just received in a BEGIN 
rule, within that same BEGIN rule.

If you receive results one record at a time, you can process each record in a 
do/while loop that emulates awk's usual per-record processing, as in this 
superficial example:

  # The record to be processed is expected in variable rec.
  do {
    $0=rec;
    if ($0~/abc/) {
      # Do whatever you need to do with abc records.
      };
    if ($0~/def/) {
      # Do whatever you need to do with def records.
      };
    if ($0~/ghi/) {
      # Do whatever you need to do with ghi records.
      # The following continue is equivalent to what awk's next statement 
usually does.
      continue;
      };
    if ($0~/jkl/) {
      # Do whatever you need to do with jkl records that are not (also) ghi 
records.
      };
    } while (0);

(Awk has a switch statement, but conventional do/while and for loops are more 
flexible, IMO.)

If you receive results a bunch of records at a time, you could save the records 
in an array, and change the do/while to a for loop.

If you need the other side effects of awk's main-loop processing (e.g., NR, RT, 
etc.) you can add code to emulate those.  If you need field splitting other 
than the usual FS-style field splitting, you can set FPAT or FIELDWIDTHS before 
setting $0 at the top.  All mutatis mutandis.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]