-2

I have this piece of code:

{ 
    if ($4 ~search) 
    {
        match ($4, /([0-9]{2}\/([a-zA-z]{3})\/([0-9]{4}))/, dates)

        print dates[1] "\t" dates[2] "\t" dates[3]
    }
}

and when it is run it produces this:

[19/Oct/2012:12:34:32
[19/Oct/2012:12:34:32
[19/Oct/2012:12:34:33

So in theory it is working and producing some of what I want but how do I get it to just produce 19/Oct/2012 any suggestions?

My questions is that I would like the output to just produce the date 19/Oct/2012?

user2160949
  • 131
  • 2
  • 9

2 Answers2

1

You need to fix your regexp:

{
    match ($4,/[0-9]{2}\/[a-zA-z]{3}\/[0-9]{4}/,date)
    print date[0] 
}

Will output:

19/Oct/2012
19/Oct/2012    
19/Oct/2012

Notes:

  • Your current regexp looks for dates with numerical month e.g 19/10/2012 not 19/Oct/2012.

  • Your capture groups are missed matched (Unbalanced parenthesis).

  • In your given example the year is 20012 not 2012!?

  • You go from dates to date.

  • date[0] stores the whole match whilst date[n] refers to the nth capture group.

There may be a better approach to your overall problem but it's impossible to say with the very little (and very erroneous) information given.

Edit:

The only issues that seems to be left is the misplaced capture group.

{
    match ($4, /([0-9]{2})\/([a-zA-z]{3})\/([0-9]{4})/, dates)    
    print dates[1] "\t" dates[2] "\t" dates[3]
}

Will output:

19    Oct   2012
19    Oct   2012
19    Oct   2012

But in your question you state you want the output in the format 19/Oct/2012 (which is what my first suggestion does)!?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • Hey thank you but this doesnt work but thanks anyway for the notes they are quite helpful. – user2160949 Mar 18 '13 at 15:30
  • @user2160949 the *new* code you added gives no more clarification and the only problem you seem to have fixed is the correct use of `dates` instead of `date` all the regexp issues I have highlighted you have not fixed. You need to update the question with sample lines of your input and describe what you are trying to do along with expected output as I have already highlighted your syntactical errors. – Chris Seymour Mar 18 '13 at 16:03
  • Hey changed it again the 20012 was a typeo, also print[0] is the same as what im printing anyway so produces same outcome – user2160949 Mar 18 '13 at 16:08
  • Instructions to get help: **1.** read my answer **2.** fix the regexp issues my answer points out *(see bullet points 1 and 2)* **3.** Update your question with some sample input, describe the problem you are trying to solve and the issues you are having and add the expected output of the script. Once you have done these 3 steps it will be clear to everybody what the problem is and how to solve it and you will get lots of great answers! – Chris Seymour Mar 18 '13 at 16:12
  • ok I can only apologise for the confusion, I have made the changes that were highlighted by sudo_O – user2160949 Mar 18 '13 at 16:38
  • @user2160949 see edit, please upvote this post if it was helpful and accept it if your problem is now solved. – Chris Seymour Mar 18 '13 at 17:16
0

Ignoring the errors in your code sample and output, if all you want to do is strip out the leading [, then you could just substr the match starting at the 2nd character:

print substr(date[0], 2)

However, I suggest revising your regular expression to output the correct match to begin with.

pestrella
  • 9,786
  • 4
  • 39
  • 44