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

Problem using BEGIN in AWK script

Expand Messages
  • Robert Bull
    I wanted a copy of my library wish list on my own PC (and Palm), so copied and pasted it into a file. It was messy, so I made a simple AWK ... # For cleaning
    Message 1 of 2 , Apr 29, 2012
    • 0 Attachment
      I wanted a copy of my library wish list on my own PC (and Palm), so
      copied and pasted it into a file. It was messy, so I made a simple AWK
      script to clean it up. The script is:

      ----------------
      # For cleaning up library wish list

      BEGIN {
      "c:\\dos\\utils\\logecho.com $C$Y-$M-$D" | getline date
      gsub(/ *$/, "", date)
      printf("Library wish list, %s\n\n", date)
      }

      /^Your Wish List$/ { $0 = "" }
      /\"Transfer to wish list\"\.$/ { $0 = "" }
      /[Bb]ook cover$/ { $0 = "" }
      /^Paperback$/ { $0 = "" }
      /^Hardback$/ { $0 = "" }
      /^Add a review$/ { $0 = "" }
      / *\* More +Details$/ { $0 = "" }
      $0 !~ /[a-zA-Z]/ { $0 = "" }

      / *\* Remove From wish list$/ {
      $0 = ""
      printf("\n")
      }
      $0 !~ /^$/ { print }
      ----------------

      This is a valid script and runs perfectly in a TCC-LE console,
      converting something like this:

      ----------------
      Your Wish List

      Use the wish list to add items you may wish to reserve or refer to at a future date. To add items to your wish list, first add them to your reservation basket and then click on "Transfer to wish list".

      Throne of Jade Book cover
      Throne of Jade

      Novik, Naomi
      Paperback
      Add a review

      * More Details
      * Remove From wish list

      Game of thrones: Book 1 of a song of ice and fir Book cover
      Game of thrones: Book 1 of a song of ice and fir ...

      Martin, George R. R.
      Paperback
      Add a review

      * More Details
      * Remove From wish list

      Firewall Book cover
      Firewall

      Mankell, Henning
      Paperback
      Add a review

      * More Details
      * Remove From wish list
      ----------------

      into this:

      ----------------
      Library wish list, 2012-04-29

      Throne of Jade
      Novik, Naomi

      Game of thrones: Book 1 of a song of ice and fir ...
      Martin, George R. R.

      Firewall
      Mankell, Henning
      ----------------

      The only part of the script that needs explaining is the BEGIN block,
      where the line

      "c:\\dos\\utils\\logecho.com $C$Y-$M-$D" | getline date

      calls LOGECHO.COM from Horst Schaeffer's PBATS collection of batch
      file enhancers, to retrieve the system date in ISO format and put it
      in variable date .

      Then I copied the AWK script into my NTP clipbook, named with header
      "Clean up library wish list" (without quotes) and made a clip to call
      it:

      Header:
      Library wish list
      Clip:
      ^!RunGawk "Clean up library wish list"

      and found an oddity. All it did was return

      ----------------
      Library wish list, 2012-04-29

      ----------------

      i.e., it correctly ran the BEGIN block, but nothing else. I found a
      workaround by removing the BEGIN block altogether and calling the
      script with a modified clip:

      ----------------
      ^!RunGawk "Clean up library wish list"
      ^!Keyboard CTRL+HOME
      ^!InsertText Library wish list, ^$GetDate(yyyy-mm-dd)$^p^p
      ----------------

      i.e., run the AWK script first, then use keyboard macro to go to top
      of file and use native NTP commands to enter the heading text and ISO
      date. This works, probably quicker than calling LOGECHO, though it
      looks cackhanded inasmuch as it puts the heading in last not first.

      Well, problem solved. But, please, why didn't the AWK script with the
      BEGIN block work?


      --
      Thanks,

      Robert Bull
      mailto:barlennan@...
    • mycroftj
      ... My knowledge of AWK GAWK is so close to zero I may be laughed out of the arena but what the heck... In my short time looking over this new language, I
      Message 2 of 2 , May 9, 2012
      • 0 Attachment
        --- In ntb-scripts@yahoogroups.com, Robert Bull <barlennan@...> wrote:
        >
        > I wanted a copy of my library wish list on my own PC (and Palm), so
        > copied and pasted it into a file. It was messy, so I made a simple AWK
        > script to clean it up. The script is:
        >
        > ----------------
        > # For cleaning up library wish list
        >
        > BEGIN {
        > "c:\\dos\\utils\\logecho.com $C$Y-$M-$D" | getline date
        > gsub(/ *$/, "", date)
        > printf("Library wish list, %s\n\n", date)
        > }
        >
        > /^Your Wish List$/ { $0 = "" }
        > /\"Transfer to wish list\"\.$/ { $0 = "" }
        > /[Bb]ook cover$/ { $0 = "" }
        > /^Paperback$/ { $0 = "" }
        > /^Hardback$/ { $0 = "" }
        > /^Add a review$/ { $0 = "" }
        > / *\* More +Details$/ { $0 = "" }
        > $0 !~ /[a-zA-Z]/ { $0 = "" }
        >
        > / *\* Remove From wish list$/ {
        > $0 = ""
        > printf("\n")
        > }
        > $0 !~ /^$/ { print }
        > ----------------
        >
        > This is a valid script and runs perfectly in a TCC-LE console,
        > converting something like this:
        >
        > ----------------
        > Your Wish List
        >
        > Use the wish list to add items you may wish to reserve or refer to at a future date. To add items to your wish list, first add them to your reservation basket and then click on "Transfer to wish list".
        >
        > Throne of Jade Book cover
        > Throne of Jade
        >
        > Novik, Naomi
        > Paperback
        > Add a review
        >
        > * More Details
        > * Remove From wish list
        >
        > Game of thrones: Book 1 of a song of ice and fir Book cover
        > Game of thrones: Book 1 of a song of ice and fir ...
        >
        > Martin, George R. R.
        > Paperback
        > Add a review
        >
        > * More Details
        > * Remove From wish list
        >
        > Firewall Book cover
        > Firewall
        >
        > Mankell, Henning
        > Paperback
        > Add a review
        >
        > * More Details
        > * Remove From wish list
        > ----------------
        >
        > into this:
        >
        > ----------------
        > Library wish list, 2012-04-29
        >
        > Throne of Jade
        > Novik, Naomi
        >
        > Game of thrones: Book 1 of a song of ice and fir ...
        > Martin, George R. R.
        >
        > Firewall
        > Mankell, Henning
        > ----------------
        >
        > The only part of the script that needs explaining is the BEGIN block,
        > where the line
        >
        > "c:\\dos\\utils\\logecho.com $C$Y-$M-$D" | getline date
        >
        > calls LOGECHO.COM from Horst Schaeffer's PBATS collection of batch
        > file enhancers, to retrieve the system date in ISO format and put it
        > in variable date .
        >
        > Then I copied the AWK script into my NTP clipbook, named with header
        > "Clean up library wish list" (without quotes) and made a clip to call
        > it:
        >
        > Header:
        > Library wish list
        > Clip:
        > ^!RunGawk "Clean up library wish list"
        >
        > and found an oddity. All it did was return
        >
        > ----------------
        > Library wish list, 2012-04-29
        >
        > ----------------
        >
        > i.e., it correctly ran the BEGIN block, but nothing else. I found a
        > workaround by removing the BEGIN block altogether and calling the
        > script with a modified clip:
        >
        > ----------------
        > ^!RunGawk "Clean up library wish list"
        > ^!Keyboard CTRL+HOME
        > ^!InsertText Library wish list, ^$GetDate(yyyy-mm-dd)$^p^p
        > ----------------
        >
        > i.e., run the AWK script first, then use keyboard macro to go to top
        > of file and use native NTP commands to enter the heading text and ISO
        > date. This works, probably quicker than calling LOGECHO, though it
        > looks cackhanded inasmuch as it puts the heading in last not first.
        >
        > Well, problem solved. But, please, why didn't the AWK script with the
        > BEGIN block work?
        >
        >
        > --
        > Thanks,
        >
        > Robert Bull
        > mailto:barlennan@...
        >


        My knowledge of AWK\GAWK is so close to zero I may be laughed out of the arena but what the heck...

        In my short time looking over this new language, I believe I saw some comments on some website somewhere that indicated there may be an issue with gsub and/or some other function that seemed very similar to it. You might want to try a version of your program without it in the BEGIN block and see what happens.

        Sorry if this is the stupidest answer of the year.

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