0

My question is kinda long Im trying to make a check in and out system simple, a menu with options 1 checkin and 2 checkout at the end of the day when I go home. My file contains:

Dato: 22-02-2018    
Mødt ind: 700    
Gået hjem: 1600    
Overtid:    
Dato: 23-02-2018    
Mødt ind: 730    
Gået hjem: 1600
Overtid:

I gotta find difference for 730 and 1600 in this file.. and if the number is higher than 800 it gotta print the number if there is a difference hope you understand my question

Félicien
  • 493
  • 3
  • 9
peter
  • 17
  • 4

1 Answers1

2

If it's the Overtid: that you want to fill out, then the following does that:

awk 'function overtime(s,e) {
         if (s) printf("Overtid: %d\n", (e - s > 800) ? e - s : 0);
     }
     /^Mødt ind:/  { start = $NF; print } 
     /^Gået hjem:/ { end   = $NF; print } 
     /^Dato:/      { overtime(start, end); print }
     END           { overtime(start, end) }' file

This picks out the start and end times from the data and for each Dato: line (which signifies a new record in the data), it prints out the overtime for the previous record. This is also done at the end (the END block).

I've done overtime() as a function as we need to do the same thing in two different places in the script. The if (s) in the function is to ensure that we don't get a bogus Overtid: 0 output when hitting the very first Dato: line in the data.

The print statements in the code passes through the existing data to the output.

Output with your data:

Dato: 22-02-2018
Mødt ind: 700
Gået hjem: 1600
Overtid: 900
Dato: 23-02-2018
Mødt ind: 730
Gået hjem: 1600
Overtid: 870
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • is that gonna make a new file or is that gonna be printed out to my original file? kinda new to all of this programming, just wanna understand it – peter Feb 23 '18 at 10:43
  • @peter This will, as it's written now, just output it on the terminal. To output to a new file, just add `>newfilename` at the end of the command (after the input filename). Do _NOT_ use the same name for the output file as for the input file. – Kusalananda Feb 23 '18 at 10:54