Hi, Iam very new to scripting. Here is the senario
I have csv file as
sl.no, name
1, har
2, kalcsv 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 belowsl.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
If you start out with an input file that looks like this: sl.no, name
1, har
2, kal
3, mal
4, paland 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
doneYou 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
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" ]
doline=`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` >> file2else
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,231110I need the unique data. Please try to find a solution for these
Thanks,
Sunkoju
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 < file12) 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
But when I am using Jerrys script, am getting an error as
1) integer not found
2) -u3 is not an identifierWhat 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...
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.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |