Having Problems with AWK

So, I'm having a lot of crazy problems with Awk that I cannot understand. This one in particular is driving me nuts. Here is one section of my Awk script:

    print $0
    
    sub(/Jan/,"",$2)
    sub(/Feb/,"",$2)
    sub(/Mar/,"",$2)
    sub(/Apr/,"",$2)
    sub("May","",$2)
    sub(/Jun/,"",$2)
    sub(/Jul/,"",$2)
    sub(/Aug/,"",$2)
    sub(/Sep/,"",$2)
    sub(/Oct/,"",$2)
    sub(/Nov/,"",$2)
    sub(/Dec/,"",$2)

    print $0

and here is the single line that is in the datafile I am working with:

Thursday, May 29, 2014,4,11,20 PM GMT-0700,1,Hard to get mario and pokemon gamecube cases ,http,//www.listia.com/auction/17243312-hard-to-get-mario-and-pokemon-gamecube

I am trying to simply delete the word, "May", and do nothing else. I have tried this a few different ways, using a double-quoted string instead of RegExp, and the output was exactly the same. Here is the output I am getting:

Thursday, May 29, 2014,4,11,20 PM GMT-0700,1,Hard to get mario and pokemon gamecube cases ,http,//www.listia.com/auction/17243312-hard-to-get-mario-and-pokemon-gamecube
Thursday   29  2014 4 11 20 PM GMT-0700 1 Hard to get mario and pokemon gamecube cases  http //www.listia.com/auction/17243312-hard-to-get-mario-and-pokemon-gamecube

You can see that after running the script, I am somehow getting a very weird result. The word "May" has been stripped from the second field, but somehow all of the commas have also been stripped from the output.

For the record I am using Gawk under Ubuntu. Any help is greatly appreciated.

---------- Post updated at 05:55 AM ---------- Previous update was at 05:43 AM ----------

Here is a more complete explanation. This is the datafile:

"Thursday, May 29, 2014","4:11:20 PM GMT-0700","1",                      "Hard to get mario and pokemon gamecube cases"           ,"http://www.listia.com/auction/17243312-hard-to-get-mario-and-pokemon-gamecube"

This is the AWK script:

{
    gsub(/\ \ /, "")
    gsub(/\"/, "")
    
    gsub(/\:/, "\,")
       
    print $0
    
    sub(/Jan/,"",$2)
    sub(/Feb/,"",$2)
    sub(/Mar/,"",$2)
    sub(/Apr/,"",$2)
    sub("May","",$2)
    sub(/Jun/,"",$2)
    sub(/Jul/,"",$2)
    sub(/Aug/,"",$2)
    sub(/Sep/,"",$2)
    sub(/Oct/,"",$2)
    sub(/Nov/,"",$2)
    sub(/Dec/,"",$2)

    print $0
    
    gsub(/" AM"/, "\,AM", $4)
    gsub(/" PM"/, "\,PM", $4)
    gsub(/" GMT"/, "\,GMT", $4)
    
    gsub(/\ /,"", $2)

    gsub(/\ /,"", $3)

}

And, here are the two output lines, printed before and after the block of code pertaining to the months above.

Thursday, May 29, 2014,4,11,20 PM GMT-0700,1,Hard to get mario and pokemon gamecube cases ,http,//www.listia.com/auction/17243312-hard-to-get-mario-and-pokemon-gamecube
Thursday   29  2014 4 11 20 PM GMT-0700 1 Hard to get mario and pokemon gamecube cases  http //www.listia.com/auction/17243312-hard-to-get-mario-and-pokemon-gamecube

The command I am using to launch awk is:

gawk -F, -f <awkfile> <datafile>

hope that helps

Since you are modifying a field ( $2 ), the record gets recomputed and all input field separators FS ( a comma) get replaced by the default output field separators OFS (a single space). Try it like this:

gawk -F, -v OFS=, -f <awkfile> <datafile>

Just curious, I have the book, "Effective Awk Programming". Suffice it to say that is does not to a very effective job at actually teaching Awk. This is all it really has to say about OFS:

"It is output between the fields output by a print statement. Its default value is " ", a string consisting of a single space.

I am just wondering if you can tell me why the first print statement in my code leaves the commas, while the second one removes them? Thank you.

Because in the first case none of the fields have been altered ( $1 .. $NF ) . The preceding gsubs operate on the record itself ( $0 ) and then the record does not get recomputed and thus FS does not get replaced by OFS .

Hi/

My copy says:

EAP 2nd Edition, page 42, A Robbins, SSC.

I think the real issue is changing the field, although I can see your point perhaps desiring that references to OFS would be more completely covered in the index.

Best wishes ... cheers, drl

Thank you guys for your help. I believe drl hit the nail on the head with the index. I originally read the Aho/Weinberger book many years ago and found it very simple and easy to follow. I misplaced the book over the years and thought I could save a buck with the Robbins book (2nd Edition), but I have also noticed that the index is very sparse, and even does not provide info for many topics that are shown prominently in the ToC.

I have company today, but as soon as I get a chance I will try these solutions and give some more thanks.