Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

bash - Converting second pattern to millisecond in awk

I have file which is having pattern 's' , I need to convert into 'ms' by multiplying by 1000. I am unable to do it. Please help me.

file.txt

First launch 1
App: +1s170ms

First launch 2
App: +186ms

First launch 3
App: +1s171ms

First launch 4
App: +1s484ms

First launch 5
App: +1s227ms

First launch 6
App: +204ms

First launch 7
App: +1s180ms

First launch 8
App: +1s177ms


First launch 9
App: +1s183ms

First launch 10
App: +1s155ms

My code:

awk 'BEGIN { FS="[: ]+"}
/:/ && $2 ~/ms$/{vals[$1]=vals[$1] OFS $2+0;next}
END {
         for (key in vals)
          print key vals[key]
}' file.txt

Expected output:

App 1170 186 1171 1484 1227 204 1180 1177 1183 1155

Output Coming:

App 1 186 1 1 1 204 1 1 1 1

How to convert in above pattern 's' to 'ms' if second pattern comes .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I will try to do here is explain it a bit generic and then apply it to your case.

Question: I have a string of the form 123a456b7c8d where the numbers are numeric integral values of any length and the letters are corresponding units. I also have conversion factors to convert from unit a,b,c,d to unit f. How can I convert this to a single quantity of unit f?

Example: from 1s183ms to 1183ms

Strategy:

  1. create per string a set of key-value pairs 'a' => 123,'b' => 456, 'c' => 7 and 'd' => 8
  2. multiply each value with the corect conversion factor
  3. add the numbers together

Assume we use awk and the key-value pairs are stored in array a with the key as an index.

  1. Extract key-value pairs from str:

    function extract(str,a,   t,k,v) {
       delete a; t=str; 
       while(t!="") { 
          v=t+0; match(t,/[a-zA-Z]+/); k=substr(t,RSTART,RLENGTH);
          t=substr(t,RSTART+RLENGTH);
          a[k]=v
       }
       return
     }
    
  2. convert and sum: here we assume we have an array f which contains the conversion factors:

    function convert(a,f,  t,k) {
       t=0; for(k in a) t+=a[k] * f[k]
       return t
    }
    
  3. The full code (for the example of the OP)

    # set conversion factors
    BEGIN{ f['s']=1000; f['ms'] = 1 }
    # print first word
    BEGIN{ printf "App:" }
    # extract string and print
    /^App/ { extract($2,a); printf OFS "%dms", convert(a,f) }
    END { printf ORS }
    

which outputs:

 App: 1170ms 186ms 1171ms 1484ms 1227ms 204ms 1180ms 1177ms 1183ms 1155ms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...