-3

File: file.txt

Iteration 1
RAM: +2s342ms (total +417ms)

Iteration 2
RAM: +2s (total +385ms)

Iteration 3
RAM: +149ms (total +185ms)

The code was taken from https://stackoverflow.com/a/54702959/10220825

try.awk

/:/ && $2 ~/ms$/{vals[$1]=vals[$1] OFS $2+0;next}
/:/ && $2 ~/[^m]s$/{vals[$1]=vals[$1] OFS ($2+0)*1000}
END {
   for (key in vals)
   print key vals[key]
}

On executing: awk -f try.awk file.txt

Expected output:

RAM: 2342 2000 149

Output:

RAM: 2 2000 149

Please help in fixing the above code to convert s to ms, the problem I am facing when the value is mixed of s and ms.

kvantour
  • 25,269
  • 4
  • 47
  • 72
Ratnesh
  • 1,554
  • 3
  • 12
  • 28
  • Hi and welcome to Stack Overflow. Have a look at [this answer](https://stackoverflow.com/a/56165560/8344060) it explains how you can do such a conversion. – kvantour May 23 '19 at 07:38
  • 1
    Looking at all the questions you have had regarding this same topic (this question, [the other one](https://stackoverflow.com/questions/54702875), [the one before](https://stackoverflow.com/questions/56164408/)), I do not know where the output comes from, but did you consider that you might also have minutes, hours and even days as output? You are currently also asking exactly the same question you asked a [few days ago](https://stackoverflow.com/questions/56164408). This is called a duplicate! I understand you try to build up a tool and you are discovering different problems every day. – kvantour May 23 '19 at 07:54
  • 1
    This also seems a lot like an [XY-problem](https://meta.stackexchange.com/questions/66377/), maybe you should ask a question describing your full problem and we might be able to help you out from beginning till end in one go. – kvantour May 23 '19 at 07:55
  • 2
    Furthermore, have a look at [Someone answers](https://stackoverflow.com/help/someone-answers), None of the questions I mentioned before have accepted answers. Did they not fulfil your requirements or is there something missing? – kvantour May 23 '19 at 07:59
  • 1
    I have edited my answer in question https://stackoverflow.com/a/54702959/10220825 to take this into account! – Allan May 23 '19 at 08:11

2 Answers2

1

Assuming that your Input_file will be always like shown samples, could you please try following.

awk '
/RAM:/{
  sub(/^\+/,"",$2)
  num=split($2,array,"[s|ms]")
  if($2 ~ /[0-9]+s[0-9]+ms|[0-9]+s$/){
     print array[1] * 1000 + array[2]
  }
  else{
     print array[1]
  }
  num=""
}
'   Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • you forgot the end part. The total should be printed on the same line. – kvantour May 23 '19 at 08:09
  • Actually this will not work for the first file `file.txt` detailed in the question https://stackoverflow.com/questions/54702875/print-file-in-particular-order-in-awk/54702959#54702959 – Allan May 23 '19 at 08:13
1

In the ms catching branch you are assuming that there will only be milliseconds. One of the things you can do is to use a regular expression to capture both optional groups in a single pass and compute your time with them:

/:/ {
    match($2, /\+(([0-9]+)s)?(([0-9]+)ms)?/, arr)
    ms_time = arr[2]*1000 + arr[4] + 0
    vals[$1]=vals[$1] OFS ms_time
}

END {
     for (key in vals)
         print key vals[key]
}

Here the line condition has been simplified and the time capturing logic has been put into the expression (you would also needed to parse that line, so no gain on separating all the cases in different expressions).

Poshi
  • 5,332
  • 3
  • 15
  • 32