1

I am parsing with awk an output file where in certain point the date is displayed in this way

08/20/2014--05:08:48 PM--357624188
...
08/20/2014--05:10:04 PM--487503599
...

I would like to know whether is possible or not to convert the string date/time into a integer because I would like to calculate the MTBF based on that time.

May anyone tell me how to achieve that casting in awk/gawk?

Thanks in advance,

Best regards!

Edit: My output should be 357624188 as an integer so that I can suctract 487503599 - 357624188

SOLVED: as @Kent was suggesting, I could solve it using

awk 'BEGIN {previousT=0}{print $NF " " previousT " " $NF - previousT; previousT=$NF;}'
5
  • What's your expected output? Commented Aug 25, 2014 at 8:35
  • My expected output would be something like 357624188 but I would like it to be a integer Commented Aug 25, 2014 at 8:36
  • 1
    @ndarkness awk -F'--' '{print $NF}' file ? :-) Commented Aug 25, 2014 at 8:38
  • 1
    ndarkness it might be better to write an answer with the solution, so that it is clear that it was solved to the future readers of the question. Also, you can ask @Kent to post it as an answer. Commented Aug 25, 2014 at 9:19
  • wrt your posted solution - you don't need to init previousT, you almost certainly do want a test so you don't print anything for the first line read, you don't need a trailing semi-colon, and you should use , instead of a hard-coded " " between output fields. e.g. awk 'prev!=""{print $NF, prev, $NF-prev} {prev=$NF}'. Commented Aug 25, 2014 at 14:09

1 Answer 1

3

you can do in this way, (data validation ignored)

awk -F'--' '{printf "%d-%d=%d\n",$NF,p,$NF-p;p=$NF}' file

test

$ echo "08/20/2014--05:08:48 PM--111      
dquote> 08/20/2014--05:08:48 PM--211      
dquote> 08/20/2014--05:08:48 PM--311"|awk -F'--' '{printf "%d-%d=%d\n",$NF,p,$NF-p;p=$NF}'
111-0=111
211-111=100
311-211=100
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.