-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?

4
  • 1
    why sometimes dates sometimes date? typo?? can you paste somemore codes? or input??
    – Kent
    Mar 18, 2013 at 14:34
  • 1
    Your regexp and your output don't match at all? Mar 18, 2013 at 14:43
  • Edit your question, and paste a few lines of input, please. The solution might be as simple as BEGIN{FS=":"}{print $1}. Without valid input, it's impossible to say. Mar 18, 2013 at 15:00
  • Edited code to make the question more understandable Mar 18, 2013 at 15:34

2 Answers 2

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)!?

6
  • Hey thank you but this doesnt work but thanks anyway for the notes they are quite helpful. Mar 18, 2013 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. Mar 18, 2013 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 Mar 18, 2013 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! Mar 18, 2013 at 16:12
  • ok I can only apologise for the confusion, I have made the changes that were highlighted by sudo_O Mar 18, 2013 at 16:38
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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.