0

When I try to grep something, I get:

09:00:02.052(0.0000<-0.0001<-0.0002)
15:04:07.225(75.8901<-78.1234<-79.4567)
16:08:11.463(100.0000<-100.0000<-100.0000)

values in () are percentage, so always lies in [0, 100]

So the input format will always be like:

HH:MM:SS.mmm(numbers1.xxxx<-numbers2.xxxx<-numbers3.xxxx)

And let's assume that the date is 20181025.

Is it possible to transform this grep result using awk/other unix command into a timeseries-like csv format:

unixtimestamp1,0.0000,0.0001,0.0002
unixtimestamp2,75.8901,78.1234,79.4567
unixtimestamp3,100.0000,100.0000,100.0000
  • It is guaranteed that numbers are always with 4 digits.

Thanks!

Amessihel
  • 5,891
  • 3
  • 16
  • 40
Tom Lau
  • 109
  • 1
  • 7
  • 1
    I'm not sure, but as per your expected output, can you try this and if this works?`sed 's#(#,#g ; s#<-#,#g ; s#)$##' file_name`....maybe there could be better ways to have the outcome.. – User123 Oct 25 '18 at 16:17
  • What do you mean by `unixtimestamp1`? Do you want to assume that `09:00:02.052` is on today's date and then convert that date time into seconds since the unix epoche `1970-01-01`? – Socowi Oct 25 '18 at 17:20
  • Hi Socowi, yes. The assumption is that 09:00:02.052 is on today's date and may need the unixtimestamp contains info of millisecond as well... ( 09:00:02.052 ). Thanks! – Tom Lau Oct 26 '18 at 13:51

1 Answers1

1

Try this line:

sed 's/^\(.\+\).(\(.\+\)<-\(.\+\)<-\(.\+\)./printf "%.3f,%s,%s,%s" "$(date  --date="\1" +"%s.%N")" \2 \3 \4/e'

Output got:

1540537202.050,0.0000,0.0001,0.0002
1540559047.220,75.8901,78.1234,79.4567
1540562891.460,100.0000,100.0000,100.0000

Note : I assumed your localization (LC_ALL) formats floats with a dot, not a comma. Otherwise, set this variable before the sed command:

LC_ALL=C sed 's/^\(.\+\).(\(.\+\)<-\(.\+\)<-\(.\+\)./printf "%.3f,%s,%s,%s" "$(date  --date="\1" +"%s.%N")" \2 \3 \4/e'

To get a timestamp from a date time string, you can use the command date. See this SO answer for more details.

To execute this command with the first captured group (the date), I used the sed e flag, inspired from this SO answer. It allows sed to get the input of a shell command passed.

More info with info sed ('sed' scripts -> The "s" Command) :

This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; results are undefined if the command to be executed contains a NUL character. This is a GNU 'sed' extension.

So it may not work outside the GNU collection.

Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • Hi Amessihel, your answer is indeed brilliant and achieve my goals! thank you very much! But 1 small issue is that: 1540476491 is a unix timestamp with accuracy of "second". The original 09:00:02.052 contains info of "millisecond". Is it possible to achieve this as well? Thanks! – Tom Lau Oct 26 '18 at 13:50