adding a date column in csv file

November 22, 2010 at 00:57:22
Specs: unix
Hi, Iam very new to scripting. Here is the senario
I have csv file as
sl.no, name
1, har
2, kal

csv file always gets updated with new data,(history data is also present and it is same as before without date)
Now, csv file should be added with a new column called "date" as shown below

sl.no, name, date
1, har
2, kal
3, mal, 22/11/2010 (coz data "3, kal" has loaded into csv on 22/11/2010)
4, pal, 23/11/2010 (coz data "4 pal" has loaded into csv on 23/11/2010)
and so on....
Hope to see a solution


See More: adding a date column in csv file

Report •

#1
November 22, 2010 at 21:44:40
If you start out with an input file that looks like this:

sl.no, name
1, har
2, kal
3, mal
4, pal

and you run this script:

#/bin/ksh

integer counter=0
exec 3< data.csv
while read -u3 line
do
if [[ $counter -eq 0 ]]
then
print "$line, date"
(( counter +=1 ))
else
print "$line, $(date +%d/%m/%Y) (coz data \"$line\" has loaded into csv on $(date +%d/%m/%Y))"
fi
done

You will get output that looks like this:

sl.no, name, date
1, har, 23/11/2010 (coz data "1, har" has loaded into csv on 23/11/2010)
2, kal, 23/11/2010 (coz data "2, kal" has loaded into csv on 23/11/2010)
3, mal, 23/11/2010 (coz data "3, mal" has loaded into csv on 23/11/2010)
4, pal, 23/11/2010 (coz data "4, pal" has loaded into csv on 23/11/2010)

If this is not what you want, then modify what I've written to suit your needs.

Jerry Lemieux


Report •

#2
November 23, 2010 at 08:17:51
Thanks Jerry. But the O/P should not be the way you have shown.
I just have a script as mentioned below. This is working, but as many times I run the script those many times the data is loading.
the script is:

#!/bin/ksh

cd /data


n=`wc -l < file1`
i=1
while [ "$i" -le "$n" ]
do

line=`cat file1 | head -$i | tail -1`
a=`echo $line | nawk 'BEGIN{FS=","};{print NF}'`


if [ "$a" -eq "2" ]
then
echo $line `date +%d%m%y` >> file2

else
echo $line
fi
i=`expr $i + 1`

done

(where file1 has
1,har
2,mal
and file2 is O/P file)

So, when I run the script for the first time, file2 is
1,har,221110
2,mal,221110
Again when i load the data into file1 with "3, kar" on 23/11/10 then again when i run the script file2 is
1,har,221110
2,mal,221110
1,har,221110
2,mal,221110
3,kar,231110

I need the unique data. Please try to find a solution for these
Thanks,
Sunkoju



Report •

#3
November 23, 2010 at 10:56:27
The biggest problem that I see is that you are not appending a comma for the third field so the file always has two fields. You probably want to do something like:

echo "$line,`date +%d%m%y`"

I have some comments about your script:

1) Why do you cycle thru file1 like this? Since you want to read file1 a line at a time, either use Jerry's method or this:

#!/bin/ksh

while read line
do
   echo "$line"
done < file1

2) I recommend that when you echo strings that you surround them with double quotes to retain the white space:

echo "$line"

instead of:

echo $line

3) Finally, although it probably doesn't hurt, there is no reason to surround integr variables in an iff comparison:

if [ $a -eq 2 ]
then


Report •

Related Solutions

#4
November 25, 2010 at 06:58:12
But when I am using Jerrys script, am getting an error as
1) integer not found
2) -u3 is not an identifier

What I need to do to remove this error
And Please let me get cleared about where can i see the output data from? Is it data.csv itself becomes an output file?

Please let me know what to do...


Report •

#5
November 25, 2010 at 16:07:29
Jerry's script sets file descriptor 3 to data.csv file. Reading file descriptors is a korn shell invention. This doesn't work with the Bourne shell. And if you have not defined your script with the korn shell invocation at line 1, column 1:

#!/bin/ksh

the script will probably be defined a a bourne shell script and fail.


Report •

Ask Question