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

552Re: [NTS] Redirecting GAWK Output

Expand Messages
  • flo.gehrke
    May 13 4:47 AM
    • 0 Attachment
      --- In ntb-scripts@yahoogroups.com, Kees Nuyt <k.nuyt@...> wrote:
      >
      > The canonical way to keep the list intact is to always include
      > a print action:
      >
      > ==========================================
      > BEGIN{
      > # init code
      > max = 0
      > outfile = "/path/to/your/output/filename.txt"
      > }
      > # echo input to original file
      > {
      > print
      > }
      > # the rest of the script
      > (length($0) > max){
      > max = length($0)
      > }
      > END{
      >
      > # choose one of these output options:
      > print max >outfile # overwrite outfile
      > print max >>outfile # append to outfile
      > close(outfile)
      > }
      > ==========================================
      >
      > As the next step, the clip can pickup the output file.
      >
      > Hope this helps.
      >
      > Regards,
      >
      > Kees Nuyt


      Kees,

      Thanks, your solution is (almost) perfectly doing the job! Also thanks to Joy for considering this problem.

      The only drawback is that we still have to create an output file. Is there no way to redirect the output from GAWK to the clipboard, for example, without saving it to the disk?

      Furthermore, it obviously is overwriting the list with the source text. Is there no way to avoid this and to leave the list completely untouched? (Without those work-arounds I described.)

      There was a similar topic in the Clip Group (Robert Bull, Using scripts in clips?, 16.10.2011, message #22161). In that discussion, diodeom described how he is combining a clip with Lua. Since it's based on '^$GetOutput$' it's similar to the idea that Joy had in mind:

      ;[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)$

      This is a solution that spares us the output file but it's no solution for getting the input just from the active document (via clipboard, ^$GetText$, ^%input% etc) and not from a file. Also, it can't call the script from inside the same clipbook but from a script file only.

      So, in my view, we could do with some more ideas for...

      - getting the input from an active document and not from a file

      - leaving that document completely unchanged

      - redirecting the output directly to NT, maybe via the clipboard, and not to an output file in order to immediately process that output with further clip commands. We've seen, of course, how to redirect it to a file, and we could easily use ^$GetFileText%, for example, to achieve this. But I still wonder if we could avoid that detour.

      Regards,
      Flo
    • Show all 16 messages in this topic