Loading ...
Sorry, an error occurred while loading the content.
 

Re: Redirecting GAWK Output

Expand Messages
  • mycroftj
    ... OK. I ve spent all morning trying to come up with a solution. I ve never done this before and have a few questions to work through on my journey. Why does
    Message 1 of 16 , May 14, 2012
      --- In ntb-scripts@yahoogroups.com, "flo.gehrke" <flo.gehrke@...> wrote:
      >
      > Hi GAWK experts!
      >
      > I can clearly see how to run a GAWK script from a NT clip. But I get into trouble whith redirecting GAWK output to a clip. Example:
      >
      > Given some lines...
      >
      > Adelaide
      > Adria
      > Karkonjafarabadi
      > Karolyne
      > Rob
      >
      > In my naive view, there should be a solution that redirects the GAWK result directly to the clipboard or a variable so the clip could call the result from there.
      >
      > Thanks for advice!
      >
      > Flo
      >


      OK. I've spent all morning trying to come up with a solution.
      I've never done this before and have a few questions to work through on my journey.


      Why does using GetOutput give me the "fstat < 0: fd = 0" line where as GetInputOutput does not?

      Why does running the GAWK code in a NT clip execute the PRINT statement but calling external files with GetOutput, GetInputOutput or running from DOS does not? (They just give me the output 16 and not the data lines in the input file.)

      Why does END { print max | command } work (when setting 'command' to 'clip' in BEGIN but END { print max | clip } does not work??



      C:\Program Files (x86)\GnuWin32\bin\gawk.exe
      C:\Program Files (x86)\NoteTab Light\Scripts\script.awk
      C:\Users\user\Documents\input.txt

      Entering this in DOS returns 16
      gawk.exe -f "C:\Program Files (x86)\NoteTab Light\Scripts\script.awk" "C:\Users\user\Documents\input.txt"

      Running this clip
      ^!Info ^$GetOutput(^$GetGawkExe$ -f "^$GetScriptPath$script.awk" "^$GetDocumentPath$input.txt")$
      gives me
      fstat < 0: fd = 0
      16
      Why don't I see the lines output by PRINT?


      Running this clip
      ^!Info ^$GetInputOutput(^$GetGawkExe$ -f "^$GetScriptPath$script.awk" "^$GetDocumentPath$input.txt")$
      gives me
      16
      Why don't I see the lines output by PRINT?


      GAWK Subroutine called in NT clip:
      This leaves all the original document lines followed by 16 on the last line. All text is selected by clip.
      # Find size of longest line
      {
      print $0
      if (length($0) > max) { max = length($0) }
      }
      END { print max }


      SCRIPT.AWK
      ----------
      # Find size of longest line
      {
      PRINT $0
      if (length($0) > max) { max = length($0) }
      }
      END { print max }


      INPUT.TXT
      ---------
      Adelaide
      Adria
      Karkonjafarabadi
      Karolyne
      Rob

      Thanks,
      Joy
    • mycroftj
      ... Did you ever get an answer to this part of this problem??? I think the problem here is the missing -f in the InsertText statement. Looks like it should
      Message 2 of 16 , May 14, 2012
        --- In ntb-scripts@yahoogroups.com, "flo.gehrke" <flo.gehrke@...> wrote:
        >
        ---snip
        >
        > ;[path to exe]
        > ^!Set %lua%=
        > ;[path to the script]
        > ^!Set %script%=
        > ;[text to process, e.g. ^$GetSelection$, ^$GetClipboard$]
        > ^!Set %input%=
        > ^!InsertText ^$GetOutput(^%lua% "^%script%" "^%input%")$
        >
        > I tried to reproduce this with GAWK but I can't get it to work. For me, GAWK doesn't seem to accept the input from that ^%input% variable.
        >
        > It works when getting the input from a file:
        >
        > ^!Info ^$GetOutput(^$GetGawkExe$ -f ^$GetScriptPath$script.awk ^$GetDocumentPath$input.txt)$
        ---snip
        > Regards,
        > Flo
        >

        Did you ever get an answer to this part of this problem???
        I think the problem here is the missing '-f' in the InsertText statement. Looks like it should be
        ^!InsertText ^$GetOutput(^%lua% -f "^%script%" "^%input%")$

        Joy
      • flo.gehrke
        ... Joy, diodeom described a working solution with Lua. IMHO, we can t speak of a missing -f in the InsertText statement : In diodeom s model, the input
        Message 3 of 16 , May 14, 2012
          --- In ntb-scripts@yahoogroups.com, "mycroftj" <mycroftj@...> wrote:
          >
          > --- In ntb-scripts@yahoogroups.com, "flo.gehrke" <flo.gehrke@> wrote:
          > >
          > ---snip
          > >
          > > ;[path to exe]
          > > ^!Set %lua%=
          > > ;[path to the script]
          > > ^!Set %script%=
          > > ;[text to process, e.g. ^$GetSelection$, ^$GetClipboard$]
          > > ^!Set %input%=
          > > ^!InsertText ^$GetOutput(^%lua% "^%script%" "^%input%")$
          > >
          > > I tried to reproduce this with GAWK but I can't get it to work.
          > > For me, GAWK doesn't seem to accept the input from that ^%input%
          > > variable.
          > >
          > > It works when getting the input from a file:
          > >
          > > ^!Info ^$GetOutput(^$GetGawkExe$ -f ^$GetScriptPath$script.awk ^$GetDocumentPath$input.txt)$
          > ---snip
          > > Regards,
          > > Flo
          > >
          >
          > Did you ever get an answer to this part of this problem???
          > I think the problem here is the missing '-f' in the InsertText statement. Looks like it should be
          > ^!InsertText ^$GetOutput(^%lua% -f "^%script%" "^%input%")$
          >
          > Joy
          >

          Joy,

          diodeom described a working solution with Lua. IMHO, we can't speak of a "missing '-f' in the InsertText statement": In diodeom's model, the input doesn't come from a file but is assigned to an %Input% variable (with ^$GetSelection$ or ^$GetClipboard$).

          The problem is that we cannot apply this model to GAWK (called with NT) because we cannot use a variable as an argument in ^$GetOutput$ in a way like...

          ^$GetOutput(^$GetGawkExe$ -f ^$GetScriptPath$script.awk ^%Input%)$

          -- unless anyone explains how to achieve that.

          I hope we'll get some replies to all your questions in message #555.

          In my latest message, I described how to redirect the GAWK output TO the clipboard. So, for your own studies, it might interest you that there's also a way to get the input FROM the clipboard. See...

          http://groups.google.com/group/comp.lang.awk/browse_thread/thread/35fe58692e3ae4fa/ff3ac224c1e6afae?lnk=gst&q=clipboard

          If we could get this to work in a NT clip then it should be even possible to adjust diodeom's model to GAWK/NT.

          Flo
        • Robert Bull
          Hello, flo.gehrke; Sunday, May 13, 2012, 10:56:52 PM, you wrote: fg Also, I think there s no need to look for an utility programm for fg sending file
          Message 4 of 16 , May 14, 2012
            Hello, flo.gehrke;

            Sunday, May 13, 2012, 10:56:52 PM, you wrote:

            fg> Also, I think there's no need to look for an utility programm for
            fg> sending file contents to clipboard. Why don't you use...

            fg> ^!SetClipboard ^$GetFileText(output.txt)$

            I didn't know that feature. Partly because I'm a long-term, if
            not expert, AWK user, I've fought shy of learning more than the
            absolute minimum of clipbook language...

            [...]

            fg> The only problem with this is that it still changes the active
            fg> document by reading and re-inserting its contents.

            If there isn't a solution, is it possible to make a workaround by
            making the last line of the clip

            ^!Keyboard CTRL+Z

            i.e., once the AWK script has overwritten your original text, restore
            it with Undo, and then paste from the clipboard?


            I find it confusing when people talk about "clips"in Notetab, in that
            it's not always clear whether an entry in the NT clipbook is meant, or
            a clip in the sense of something copied or cut to the clipboard.

            --
            Regards,

            Robert Bull
            mailto:barlennan@...
          • mycroftj
            ... Accchh. Sorry about that. You are correct about the -f. I did see the input was from a variable then forgot later on. Between testing different versions of
            Message 5 of 16 , May 14, 2012
              --- In ntb-scripts@yahoogroups.com, "flo.gehrke" <flo.gehrke@...> wrote:
              >
              > --- In ntb-scripts@yahoogroups.com, "mycroftj" <mycroftj@> wrote:
              > >
              > > --- In ntb-scripts@yahoogroups.com, "flo.gehrke" <flo.gehrke@> wrote:
              > > >
              > > ---snip
              > > >
              > > > ;[path to exe]
              > > > ^!Set %lua%=
              > > > ;[path to the script]
              > > > ^!Set %script%=
              > > > ;[text to process, e.g. ^$GetSelection$, ^$GetClipboard$]
              > > > ^!Set %input%=
              > > > ^!InsertText ^$GetOutput(^%lua% "^%script%" "^%input%")$
              > > >
              > > > I tried to reproduce this with GAWK but I can't get it to work.
              > > > For me, GAWK doesn't seem to accept the input from that ^%input%
              > > > variable.
              > > >
              > > > It works when getting the input from a file:
              > > >
              > > > ^!Info ^$GetOutput(^$GetGawkExe$ -f ^$GetScriptPath$script.awk ^$GetDocumentPath$input.txt)$
              > > ---snip
              > > > Regards,
              > > > Flo
              > > >
              > >
              > > Did you ever get an answer to this part of this problem???
              > > I think the problem here is the missing '-f' in the InsertText statement. Looks like it should be
              > > ^!InsertText ^$GetOutput(^%lua% -f "^%script%" "^%input%")$
              > >
              > > Joy
              > >
              >
              > Joy,
              >
              > diodeom described a working solution with Lua. IMHO, we can't speak of a "missing '-f' in the InsertText statement": In diodeom's model, the input doesn't come from a file but is assigned to an %Input% variable (with ^$GetSelection$ or ^$GetClipboard$).
              >
              > The problem is that we cannot apply this model to GAWK (called with NT) because we cannot use a variable as an argument in ^$GetOutput$ in a way like...
              >
              > ^$GetOutput(^$GetGawkExe$ -f ^$GetScriptPath$script.awk ^%Input%)$
              >
              > -- unless anyone explains how to achieve that.
              >
              > I hope we'll get some replies to all your questions in message #555.
              >
              > In my latest message, I described how to redirect the GAWK output TO the clipboard. So, for your own studies, it might interest you that there's also a way to get the input FROM the clipboard. See...
              >
              > http://groups.google.com/group/comp.lang.awk/browse_thread/thread/35fe58692e3ae4fa/ff3ac224c1e6afae?lnk=gst&q=clipboard
              >
              > If we could get this to work in a NT clip then it should be even possible to adjust diodeom's model to GAWK/NT.
              >
              > Flo
              >

              Accchh. Sorry about that. You are correct about the -f. I did see the input was from a variable then forgot later on. Between testing different versions of clips, external subs, running GAWK from DOS, learning GAWK syntax, etc. I was getting quite dizzy.

              I did see that message you referenced but didn't try it. Did you? At this point, I'm feeling the best way is to use your original code or, if it is applicable, my solution of echoing all the lines back out with the results as the last line.

              This is Windows, not UNIX and unless you have a good reason (which I'm sure you do) the solution you are looking for might be more effort than it is worth for most situations.

              I haven't given up. Ideas do come to me in the middle of the night.

              Joy
            • flo.gehrke
              ... Robert, Yes, ^!Keyboard CTRL+Z or ^!Toolbar Undo is a good workaround -- at least in this specific example. Nevertheless, I m looking forward to see a
              Message 6 of 16 , May 14, 2012
                --- In ntb-scripts@yahoogroups.com, Robert Bull <barlennan@...> wrote:
                >
                > fg> The only problem with this is that it still changes the active
                > fg> document by reading and re-inserting its contents.
                >
                > If there isn't a solution, is it possible to make a workaround by
                > making the last line of the clip
                >
                > ^!Keyboard CTRL+Z
                >
                > i.e., once the AWK script has overwritten your original text,
                > restore it with Undo, and then paste from the clipboard?

                Robert,

                Yes, '^!Keyboard CTRL+Z' or '^!Toolbar Undo' is a good workaround -- at least in this specific example.

                Nevertheless, I'm looking forward to see a solution where a selection from the active document is copied to the clipboard and GAWK is taking the contents of the clipboard as input data -- see my latest reply to Joy with that link to comp.lang.awk.

                Could you show us how to implement that procedure to our sample clip?

                Regards,
                Flo
              • Robert Bull
                Hello, flo.gehrke; Monday, May 14, 2012, 11:53:02 PM, you wrote: fg Nevertheless, I m looking forward to see a solution where a fg selection from the active
                Message 7 of 16 , May 16, 2012
                  Hello, flo.gehrke;

                  Monday, May 14, 2012, 11:53:02 PM, you wrote:

                  fg> Nevertheless, I'm looking forward to see a solution where a
                  fg> selection from the active document is copied to the clipboard and
                  fg> GAWK is taking the contents of the clipboard as input data -- see
                  fg> my latest reply to Joy with that link to comp.lang.awk.

                  fg> Could you show us how to implement that procedure to our sample
                  fg> clip?

                  I can't see you last message to Joy. Is it in one of the other NT
                  groups?


                  Let me go back to the beginning and ask what you're trying to achieve.
                  Are you:

                  1) Making tests to help in understanding how NTP's clipbook interacts
                  with the Windows clipboard? If so, I don't know enough clipbook
                  programming to be able to help.

                  2) Wanting to open the file of words in NTP, run the AWK script on it
                  to find the maximum line length, and, leaving the original text
                  unchanged, append the max line length information to it? If so, I
                  think I know how to do that, and further think you're making it too
                  complicated.

                  Kees' script does the job. What I've done is put my version of it
                  (pedantically modified to make it clearer to me) in a NTP "script
                  clip" (my name for it), and call it from another "real clip." I have
                  a section titled Scripts in my clipbook, beneath the section of
                  ordinary clips. In the Scripts section I have a clip entitled "Kees
                  AWK script". It contains my version of Kees' script:

                  Kees AWK script
                  ----------------
                  BEGIN { max = 0 + 0 }
                  # echo input to original file
                  {
                  print
                  }
                  # the rest of the script
                  {
                  if(length($0) > max){
                  max = length($0)
                  }
                  }
                  END { printf("max line length is %d", max) }
                  ----------------
                  Notice there aren't any references to files or file names at all.

                  The clip that calls this script has to be called by another name -
                  very important - here it's "Test - Kees", containing just the calling
                  line:

                  Test - Kees
                  ----------------
                  ^!RunGawk "Kees AWK script"
                  ----------------

                  When I have your test data open in an NTP file and run the "Test -
                  Kees" clip *making sure to avoid calling "Kees AWK script" by name, I
                  get:

                  ----------------
                  Adelaide
                  Adria
                  Karkonjafarabadi
                  Karolyne
                  Rob
                  max line length is 16
                  ----------------

                  To prove it, I made a Jing Screencast video that you can see at
                  http://screencast.com/t/TW7PYAbQL9

                  Note that I usually activate clips by pressing Esc and choosing from
                  the dropdown menu. I'd already tested the clip, so the lightbar is
                  already on the required clip.

                  Apologies if I have still misunderstood what you were looking for.

                  --
                  Regards,

                  Robert Bull
                  mailto:barlennan@...
                • flo.gehrke
                  ... Thanks, Robert! I see that last version working and getting to the result that you had in mind. Actually, I never talked of inserting the result into the
                  Message 8 of 16 , May 17, 2012
                    --- In ntb-scripts@yahoogroups.com, Robert Bull <barlennan@...> wrote:
                    >
                    > Kees' script does the job (...) I have a section titled Scripts in
                    > my clipbook, beneath the section of ordinary clips. In the Scripts
                    > section I have a clip entitled "Kees AWK script". It contains my
                    > version of Kees' script:
                    >
                    > Kees AWK script
                    > ----------------
                    > BEGIN { max = 0 + 0 }
                    > # echo input to original file
                    > {
                    > print
                    > }
                    > # the rest of the script
                    > {
                    > if(length($0) > max){
                    > max = length($0)
                    > }
                    > }
                    > END { printf("max line length is %d", max) }
                    > ----------------
                    > Notice there aren't any references to files or file names at all.

                    Thanks, Robert!

                    I see that last version working and getting to the result that you had in mind.

                    Actually, I never talked of inserting the result into the active document. The goal was...

                    - To have that list (source text) open

                    - to run a clip which calls GAWK and starts a GAWK script which calculates the length of the longest line

                    - to redirect the result from GAWK back into the NT clip

                    - to continue clip execution with that data, i.e., in this case, just the value '16'.

                    In this context, I mentioned the clipboard only as a target for redirecting the GAWK output. The idea was, to continue clip execution, for example, with displaying the result in an infobox that could call the clipboard with...

                    ^!Info The longest line has ^$GetClipboard$ characters

                    Meanwhile, we saw approaches how to redirect the GAWK output to the clipboard (with MS Clip.exe). Also, how to direct the input into GAWK from the clipboard (with gclip.exe from UnxUtils). This could be of interest in cases where we send data from NT to the clipboard, process them with GAWK, and redirect the result via the clipboard back into a clip execution.

                    So far, we didn't see a solution for this redirection (to clipboard/from clipboard). That's why I remembered my message #557.

                    If you still have the patience to spend some time with that subject -- please have a look at the latest messages. It's all in the NT Scripts Group on Yahoo -- or in your incoming emails ;-)

                    Regards,
                    Flo
                  • Robert Bull
                    Hello, flo.gehrke; Thursday, May 17, 2012, 8:17:01 PM, you wrote: fg I see that last version working and getting to the result that you fg had in mind. What
                    Message 9 of 16 , May 17, 2012
                      Hello, flo.gehrke;

                      Thursday, May 17, 2012, 8:17:01 PM, you wrote:

                      fg> I see that last version working and getting to the result that you
                      fg> had in mind.

                      What a pity it wasn't what *you* had in mind. I might have known I'd
                      get the wrong end of the stick... still, it was a fun play session.


                      --
                      Regards,

                      Robert Bull
                      mailto:barlennan@...
                    • Kees Nuyt
                      ... PRINT is not the same as print . gawk is case-sensitive. -- Regards, Kees Nuyt
                      Message 10 of 16 , May 17, 2012
                        On Mon, 14 May 2012 16:17:09 -0000, you wrote:

                        > Why don't I see the lines output by PRINT?
                        >
                        >
                        > GAWK Subroutine called in NT clip:
                        > This leaves all the original document lines followed by 16 on the last line. All text is selected by clip.
                        > # Find size of longest line
                        > {
                        > print $0
                        > if (length($0) > max) { max = length($0) }
                        > }
                        > END { print max }
                        >
                        >
                        > SCRIPT.AWK
                        > ----------
                        > # Find size of longest line
                        > {
                        > PRINT $0
                        > if (length($0) > max) { max = length($0) }
                        > }
                        > END { print max }

                        PRINT is not the same as print .
                        gawk is case-sensitive.


                        --
                        Regards,

                        Kees Nuyt
                      Your message has been successfully submitted and would be delivered to recipients shortly.