Hello I am trying to output a CSV file and I keep getting part of my string written onto a new line.
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 and outputs the filem.
#!/bin/bash
OLDIFS=$IFS
IFS=","
cat test.csv | while read Host AName Resource MName TimeStamp Integer_Value Epoch;
do
Epoch=$(date -d "$TimeStamp GMT" +%s)
if [ -z "$Epoch" ]
then
(echo "$Host, $AName, $Resource, $MName, $TimeStamp, $Integer_Value, Epoch,";) >> target.csv
else
(echo "$Host, $AName, $Resource, $MName, $TimeStamp, $Integer_Value, $Epoch,";) >> target.csv
fi
done
I am trying to set a header then write out the appended variable, expect, and this only happens on the new value, it drops the appended variable to a new line.
#Host, AName, Resource, MName, Actual Start Time, Integer Value
, Epoch,
ABCD89A, Admin, shop, Stall Count, 2014-01-06 09:00:00.0, 0
, 1388998800,
Instead of
#Host, AName, Resource, MName, Actual Start Time, Integer Value, Epoch,
ABCD89A, Admin, shop, Stall Count, 2014-01-06 09:00:00.0, 0, 1388998800,
When I move the order around it doesn't happen. Sorry I know this is probably simple I new to Unix scripting.
EDIT
I have now changed the code to:
#!/bin/bash
OLDIFS=$IFS
IFS=","
while read Host AName Resource MName TimeStamp Integer_Value Epoch
do
Epoch=$(date -d "$TimeStamp GMT" +%s)
if [ -z "$Epoch" ]
then
echo "$Host, $AName, $Resource, $MName, $TimeStamp, $Integer_Value, Epoch,"
else
echo "$Host, $AName, $Resource, $MName, $TimeStamp, $Integer_Value, $Epoch,"
fi
done < test.csv > target.csv
And i am still getting the same problems.
also as an additional question if anyone knows why I get : command not found date: invalid date `Actual Start TimeStamp GMT' when running the date part but it produces the correct date and the scripts run.