Convert Second Column Date Into EPOCH Time And Print Complete Row

Hello Team,

I am stuck in getting the required output in the following case. Please help.

My input file is

aa|08/01/2016
bb|08/15/2016

I wish to convert the file into

aa|epoch time
bb|epoch time

I am using following code:

awk -F"|" '{print $1,"|",$d}' d="`date -d $2 '+%s'` a.csv

Thanks
Angsuman

Try

[akshay@localhost tmp]$ cat f
aa|08/01/2016
bb|08/15/2016
[akshay@localhost tmp]$ awk 'BEGIN{FS=OFS="|"}{split($2,d,/\//);$2=mktime(d[3] " " d[2] " " d[1] " " "00 00 00")}1' f
aa|1452211200
bb|1488931200
1 Like

Thank you Akshy for the help.

It worked perfectly.

Hi.

Here are two alternate solutions:

#!/usr/bin/env bash

# @(#) s2       Demonstrate date conversion, colwise-epoch, dconv.
# colwise is part of:
# http://www1.cuni.cz/~obo/textutils/   (verified 2016.08.18)
# dateutils.dconv is available in repositories and
# https://github.com/hroptatyr/dateutils        (verified 2016.08.18)

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C colwise my-epoch dateutils.dconv

FILE=${1-data1}

# Input date format:
# 08/01/2016

pl " Input data file $FILE:"
cat $FILE

pl " Input file, modified separator:"
sed 's/|/,/g' $FILE |
tee f1

pl " Results, (local) \"my-epoch\", replace delimiter after done:"
colwise --delim=',' 2 " my-epoch -e " < f1
# colwise --delim='|' 2 " my-epoch -e " < $FILE # Fails, "|" issue ?

pl " Results, dateutils.dconv:"
dateutils.dconv -S -i "%m/%d/%Y" -f "%s" < $FILE

exit 0

producing:

$ ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
colwise - ( local: RepRev 1.2, ~/bin/colwise, 2014-03-20 )
my-epoch (local) 1.7
dateutils.dconv dconv 0.3.1

-----
 Input data file data1:
aa|08/01/2016
bb|08/15/2016

-----
 Input file, modified separator:
aa,08/01/2016
bb,08/15/2016

-----
 Results, (local) "my-epoch", replace delimiter after done:
aa,1470027600
bb,1471237200

-----
 Results, dateutils.dconv:
aa|1470009600
bb|1471219200

The first uses a technique for extracting a field, running a command on that field, and replacing the original with the result. Any command may be used, I used a command we have here that does a simple date lookup, relieving us of the tedium of looking up date formats. The extraction-replacement code is a perl program, one of many in the missing-textutils collection, q.v. (It had trouble with "|", so I changed that to a ",", which can be easily changed back, but rather a pain to need to do.)

The second uses a command from a suite available in many repositories and in github.

Best wishes ... cheers, drl