Fetching data from .csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetching data from .csv file
# 1  
Old 06-24-2011
Java Fetching data from .csv file

Hi Experts,
I have created a table with columns as empname,empid,phone,shiftname. Now I am having a .csv file format which contains the shift datas of the employees. I have to fetch this file and compare with the table I created to send an alert to the specified user.
# 2  
Old 06-24-2011
Can you please post sample input and desired output; it will help us help you. Please use code tags when posting the sample code/output.
# 3  
Old 06-24-2011
I have publish this solution in this forum previously, but here it's again generic template to start csv parsing. If you need also " " support/parsing then need litle more.

Code:
#!/bin/kh or /bin/bash ...
#csvparse

# some sample.csv
echo "$(<<EOF 
name;age;girlfriend;random
aa;11;yes;$RANDOM
bb;22;yes;$RANDOM
cc;33;no;$RANDOM
dd;44;no;$RANDOM
ee;55;yes;$RANDOM
ff;44;
;44;no;$RANDOM
aa;55;no;xx;zz
EOF
)" > sample.csv

### csvparser
lastfld=0
flds[0]=""
val[0]=""
oifs="$IFS"
deli=";"
defdeli=";"
outdeli="$deli"
str=""

#####################################
initcolnames()
{
    Xcnt=$1
    Xdeli=$2
    Xstr=""
    Xfld=1
    while ((Xfld <= Xcnt ))
    do
        Xstr="${Xstr}col${Xfld}$Xdeli"
        ((Xfld+=1))
    done
    echo "$Xstr"
}
#####################################
setvar()
{
   str="$*"
   IFS="$deli"
   val=($str)
   IFS="$oifs"
   last=${#val[@]}
   f=0
   while ((f<=last && f<=lastfld))
   do
        var="${flds[$f]}"
        value="${val[$f]}"
        eval $var=\"$value\"
        ((f+=1))
   done

}

#####################################
showvar()
{
   f=0
   outstr=""
   Xoutdeli="$outdeli"
   while ((f<=lastfld))
   do
        var="${flds[$f]}"
        (( f == lastfld )) && Xoutdeli=""
        eval value=\""\$$var"\"
        outstr="$outstr$value$Xoutdeli"
        ((f+=1))
   done
   echo "$outstr"
}
#####################################
clrvar()
{
   f=0
   while ((f<=lastfld))
   do
        var="${flds[$f]}"
        eval $var=\"\"
        ((f+=1))
   done
}

#####################################
colnames()
{
    nf=0
    while [ $# -gt 0 ]
    do
        opt="$1"
        case "$opt" in
                -c) nf=$2       # no colnames, create
                   shift
                   ;;
                -d) defdeli=$2
                   shift
                   ;;
                *) str="$*" ; break ;;
        esac
        shift
    done

    (( nf > 0 )) && str=$(initcolnames $nf "$defdeli" )
    IFS="$deli"
    flds=($str)
    IFS="$oifs"
    lastfld=${#flds[@]}
    ((lastfld-=1))

}
#####################################
# MAIN
#####################################

deli=";"   # set input delimeter , or use -d option in colnames
outdeli=" | "
lineno=0
while read line
do
    ((lineno+=1))
    (( lineno == 1 )) && colnames "$line" && continue  # headerline = variable names
    clrvar
    setvar "$line"
    echo "________________________________"
    showvar

    # example using values
    [ "$name" = "cc" ] && echo "$name, age:$age"

done < sample.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetching the required data out of a tabular form

Hello Gurus, I am trying to fetch a required number of lines from an output of a command which is in tabular form. Below is the command for reference along with how the result is being shown on UNIX shell. /usr/openv/volmgr/bin/vmquery -b -p 5 The result of the above command is as... (6 Replies)
Discussion started by: Ali Sarwar
6 Replies

2. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Fetching data particular data from the file.

Hi All, I have one fine which contaning the record as below, I need to fetch the number which is in Bold and from second line only URL need to pick. :ABCD C7QyzBXIs58k 1 fdmlksdlkfn ... (4 Replies)
Discussion started by: Riverstone
4 Replies

4. Shell Programming and Scripting

Fetching data from file

Hi All, I'm facing issue while using script. Chk_Etl_Status=`cat /dstage/questnet/qnetdv/input/Etl_Status.dat|cut -d"," -f1` echo Chk_Etl_Status=$Chk_Etl_Status above result is giving me sometime value as 1 and sometime error message as Chk_Etl_Status= dsjoblaunch.sh: test: Specify a... (5 Replies)
Discussion started by: Amit786
5 Replies

5. Shell Programming and Scripting

Fetching values in CSV file based on column name

input.csv: Field1,Field2,Field3,Field4,Field4 abc ,123 ,xyz ,000 ,pqr mno ,123 ,dfr ,111 ,bbb output: Field2,Field4 123 ,000 123 ,111 how to fetch the values of Field4 where Field2='123' I don't want to fetch the values based on column position. Instead want to... (10 Replies)
Discussion started by: bharathbangalor
10 Replies

6. Shell Programming and Scripting

Fetching columns from .csv file except last column

Hi, i have below list of files so i just want the name of the files in one parameter and not the timestamp. i want only GIDW_Dy_Tm_Seg_Sls_legacy_PL_0_0_ in variable of all files. GIDW_Dy_Tm_Seg_Sls_legacy_PL_0_0_20131001101800.csv GIDW_Dly_Sls_legacy_RO_0_0_20131001172001.csv ... (9 Replies)
Discussion started by: renuk
9 Replies

7. Shell Programming and Scripting

Problems with fetching data from Oracle database

Here's a database query which looks up the NAME column of PRODUCT table SELECT NAME FROM PRODUCT ; And this query retrieves me the following output SUGAR COCOA HONEY WHEAT CABBAGE CAULI FLOWER Please note the last record CAULI FLOWER contains TWO blank spaces between the two words. ... (4 Replies)
Discussion started by: kumarjt
4 Replies

8. Shell Programming and Scripting

Formatting Report and Reading data and fetching the details from contents file

Data I was trying to write shell script which will be return the output in the below format First i was trying to do these using sed. sed -n '/.ksh/p' mainksh.ksh sed -e 's/*\(.*\)/\1/g' mainksh.ksh $RUN_DIR, $SUB_DIR and the variables which will be defined in the profile file. when i am... (0 Replies)
Discussion started by: rameshds
0 Replies

9. Programming

Problem FETCHing Long data type using CURSOR

Currently my Pro*c program is fetching a cloumn which is defined as LONG in oracle db. The data in the column is around 65k. But when I am FETCHing it to a varchar variable, I am only getting 22751 bytes of data using cursor. Is there any limitation on the data which is fetched by a cursor in... (2 Replies)
Discussion started by: manbt
2 Replies

10. Shell Programming and Scripting

fetching data from sybase using perl

How can I fetch and retreive data(of each result set) of multiple result set from sybase by calling a stored procedure using perl DBI module on an unix solaris environment????? Please help (3 Replies)
Discussion started by: wadhwa.pooja
3 Replies
Login or Register to Ask a Question