Convert Second Column Date Into EPOCH Time And Print Complete Row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Second Column Date Into EPOCH Time And Print Complete Row
# 1  
Old 08-17-2016
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

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

I wish to convert the file into

Code:
aa|epoch time
bb|epoch time

I am using following code:

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

Thanks
Angsuman



Moderator's Comments:
Mod Comment Please use CODE tags correctly ([...], NOT <...>) as required by forum rules!

Last edited by RudiC; 08-17-2016 at 09:39 AM.. Reason: Changed CODE tags.
# 2  
Old 08-17-2016
Try

Code:
[akshay@localhost tmp]$ cat f
aa|08/01/2016
bb|08/15/2016

Code:
[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

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 08-17-2016
Thank you Akshy for the help.

It worked perfectly.
# 4  
Old 08-18-2016
Hi.

Here are two alternate solutions:
Code:
#!/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:
Code:
$ ./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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a string to epoch time

Team, I am working on a shell script and i am extracting a date string in "SunOS server" with below format. Mon Jan 21 04:13:48 EST 2021 Can you please assist me the best way to convert the extracted string to epoch time like "date +%s" in Linux. Thanks in advance (1 Reply)
Discussion started by: Girish19
1 Replies

2. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. Shell Programming and Scripting

Convert epoch time to Julian date

Need assistance in converting an epoch time to Julian date To get epoch perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. Shell Programming and Scripting

Convert to epoch time

how can i modify the following command to instead provide the epoch time of the interfaces file? perl -le 'print scalar localtime ((stat "/home/skysmart/interfaces"))' Tue Feb 19 03:44:52 2013 i'm hoping to get the equivalent of this command: stat --format=%Y /home/skysmart/interfaces ... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

6. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

7. Shell Programming and Scripting

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

8. Shell Programming and Scripting

how to convert date time to epoch time in solaris

Hi, Is there any easy way to convert date time(stored in shell variable ) to epoch time in solaris box? As +%s is working on linux but not on solaris, also -d option is not working. Any suggestion please? (6 Replies)
Discussion started by: anshuman0507
6 Replies

9. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

10. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies
Login or Register to Ask a Question