Home computickets subtraction time in lines using awk, perl or other single-line

subtraction time in lines using awk, perl or other single-line

Author

Date

Category

Tell me, please, what way can you solve the following task using a single-handed one?

Task is to deduct the value from the first column of the previous building from the first column of the current line and placing it before the current line.

There is a file, type:

$ cat blabla
13: 06: 27.578195 BLABLABLA
13: 06: 27.578243 Bla blala
13: 06: 27.578271 bla
13: 06: 27.578337 ZZZ
13: 06: 27.578372 ZZZ ZZZ ZZZ
13: 06: 27.578372 ZZZ ZZZ ZZZ
13: 07: 02.224100 AAA BBB CCC

you need using any command (s) in one line to obtain a conclusion of the form:

0 13: 06: 27.578195 Blablabla
0.000048 13: 06: 27.578243 Bla bla
0.000028 13: 06: 27.578271 bla
0.000066 13: 06: 27.578337 ZZZ
0.000035 13: 06: 27.578372 ZZZ ZZZ ZZZ
34,645728 13: 07: 02.224100 AAA BBB CCC

It seems that this can be done using AWK , but I did not work.


Answer 1, Authority 100%

not deprived of drawbacks, but still method

$ perl -ane '$ c = `date +% s.% N -d $ F [0]`; Printf "% .6f% s", $ C - ($ p or $ c), $ _; $ p = $ C '& LT; log.txt
0.000000 13: 06: 27.578195 BLABLABLA
0.000048 13: 06: 27.578243 Bla bla
0.000028 13: 06: 27.578271 bla
0.000066 13: 06: 27.578337 ZZZ
0.000035 13: 06: 27.578372 ZZZ ZZZ ZZZ
0.00000000 13: 06: 27.578372 ZZZ ZZZ ZZZ
34.645728 13: 07: 02.224100 AAA BBB CCC

Keep in mind that on the border of the day, the displacement will turn out negative.


Answer 2

For example, using the GNU / AWK program.

Come on the first word on the part on the separator: , we transform the minutes-hours per second (multiplying sequentially by 60).

If the resulting value is less than the previous one, we assume that these are new days, and subtract the number of seconds in the days from the previous value.

awk 'begin {p = 0}
{
 Split ($ 1, a, ":");
 s = 0;
 For (i = 1; i & lt; = length (a); i ++) {
  S = S * 60 + A [i]
 };
 if (p == 0) {
  PrintF "0"
 } else {
  if (p & gt; s) {
   p = p - 60 * 60 * 24;
  }
  Printf "% .6f", S - P
 };
 p = s;
 Print $ 0.
} '

From Data Set:

13: 06: 27.578195 BLABLABLA
13: 06: 27.578243 Bla blala
13: 06: 27.578271 bla
13: 06: 27.578337 ZZZ
13: 06: 27.578372 ZZZ ZZZ ZZZ
13: 06: 27.578372 ZZZ ZZZ ZZZ
13: 07: 02.224100 AAA BBB CCC
01: 07: 02.224109 New day

It turns out this:

0 13: 06: 27.578195 Blablabla
0.000048 13: 06: 27.578243 Bla bla
0.000028 13: 06: 27.578271 bla
0.000066 13: 06: 27.578337 ZZZ
0.000035 13: 06: 27.578372 ZZZ ZZZ ZZZ
0.00000000 13: 06: 27.578372 ZZZ ZZZ ZZZ
34.645728 13: 07: 02.224100 AAA BBB CCC
43200.000009 01: 07: 02.224109 New day

To get a single-stroke, just remove all spaces (except in the PrintF function argument) and new lines:

awk 'begin {p = 0} {split ($ 1, a, ":"); s = 0; for ( i = 1; i & lt; = length (a); i ++) {s = s * 60 + a [i]}; if (p == 0) {printf "0"} else {if (p & gt; s) {p = P-60 * 60 * 24;} Printf "%. 6f", SP}; P = S; Print $ 0} '

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions