QuinsUK
QuinsUK

Reputation: 371

Converting GMT Time to Epoch script

I am currently trying to convert a GMT time into Unix Epoch time. The overall script reads in a CSV file, which has a time stamp, converts it and then appends the Epoch time to the end of the line as a new variable.

I looked a number of posts and read the web page on DATE http://www.linuxmanpages.com/man1/date.1.php and would have assumed that this would work.

#!/bin/bash 
OLDIFS=$IFS 
IFS=","
cat test.csv | while read Host AName Resource MName TimeStamp Integer_Value; 
do 

Epoch=date -d "$TimeStamp" +%s

echo "Host: $Host, Name: $AName, Resource: $Resourse, MName: $MName, TimeStamp: $TimeStamp, Integer_Value: $Integer_Value, Epoch: $Epoch";



done

When this is run though the script it says that the -d command isn't found I was wondering if someone would be able to help me as to why this might be.

Upvotes: 0

Views: 723

Answers (2)

keiichi
keiichi

Reputation: 83

You have to write

Epoch=`date -d "$TimeStamp" +%s`

or

Epoch=$(date -d "$TimeStamp" +%s)

Upvotes: 2

fedorqui
fedorqui

Reputation: 290215

To store the value of a command into a variable, use the var=$(command) syntax. In your case:

Epoch=$(date -d "$TimeStamp" +%s)

Also, your cat file | while read... syntax could be simplified to a while do ... done < file:

IFS="," while read Host AName Resource MName TimeStamp Integer_Value; 
do 
...
done < test.csv

Upvotes: 2

Related Questions