user2274114
user2274114

Reputation: 27

Subtract the Values of Two Columns Using Awk or Bash?

Here's my file:

$ cat myfile.txt 
48700039|09:39:58|09:40:34
48700040|09:59:12|09:59:42
48700041|10:01:08|10:05:47
48700042|10:50:53|10:51:24

I want to subtract column 2 from 3, so my desired output would be:

48700039|09:39:58|09:40:34|00:00:36
48700040|09:59:12|09:59:42|00:00:30
48700041|10:01:08|10:05:47|00:04:39
48700042|10:50:53|10:51:24|00:00:31

I've tried everything... thanks in advance! =)

Upvotes: 1

Views: 1523

Answers (3)

rpax
rpax

Reputation: 4496

Using sed and date,

while read line;do
    #extraction of the columns 2 and 3 && converting them into an array
    d=($(echo $line | sed -e 's/\(.*\)|\(.*\)|\(.*\).*/\2\n\3/')) 
    #substraction of dates
    datediff=$(($(date -d ${d[0]} +%s)-$(date -d ${d[1]} +%s))) 
    #absolute value
    datediff=${datediff/-/}
    #print the line and the 'extra' H:M:S
    printf "%s|%02d:%02d:%02d\n" $line $((datediff/3600)) $((datediff%3600/60)) $((datediff%60))
done < myfile.txt

Output:

48700039|09:39:58|09:40:34|00:00:36
48700040|09:59:12|09:59:42|00:00:30
48700041|10:01:08|10:05:47|00:04:39
48700042|10:50:53|10:51:24|00:00:31

Upvotes: 0

anubhava
anubhava

Reputation: 784998

GNU awk (gawk) based solution:

gawk -F"|" 'BEGIN {DT="2000 01 01 "} { gsub(/:/, " "); t1=mktime(DT $2);
      t2=mktime(DT $3); gsub(/ /, ":"); print $0"|"strftime("%T", t2-t1, "UTC") }'

Live Demo: http://ideone.com/VZ1DPD

Upvotes: 0

Fredrik Pihl
Fredrik Pihl

Reputation: 45644

Sometimes it is necessary to leave the wonderful world of awk/sed/bash and move into a scripting language that understand times. This is such an occasion.

#!/usr/bin/env python

from datetime import datetime

FMT = '%H:%M:%S'

with open("myfile.txt") as fd:
    for line in fd:
        line = line.strip()
        t = line.split('|')
        tdelta = datetime.strptime(t[2], FMT) - \
                datetime.strptime(t[1], FMT)

        print "%s|%s" % (line, tdelta)

output:

48700039|09:39:58|09:40:34|0:00:36
48700040|09:59:12|09:59:42|0:00:30
48700041|10:01:08|10:05:47|0:04:39
48700042|10:50:53|10:51:24|0:00:31

Upvotes: 1

Related Questions