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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT SUNOS
endusershell
getusershell(3C) Standard C Library Functions getusershell(3C)NAME
getusershell, setusershell, endusershell - get legal user shells
SYNOPSIS
#include <unistd.h>
char *getusershell(void);
void setusershell(void);
void endusershell(void);
DESCRIPTION
The getusershell() function returns a pointer to a legal user shell as defined by the system manager in the file /etc/shells. If
/etc/shells does not exist, the following locations of the standard system shells are used in its place:
/bin/bash /bin/csh
/bin/jsh /bin/ksh
/bin/pfcsh /bin/pfksh
/bin/pfsh /bin/sh
/bin/tcsh /bin/zsh
/sbin/jsh /sbin/pfsh
/sbin/sh /usr/bin/bash
/usr/bin/csh /usr/bin/jsh
/usr/bin/ksh /usr/bin/pfcsh
/usr/bin/pfksh /usr/bin/pfsh
/usr/bin/sh /usr/bin/tcsh
/usr/bin/zsh /usr/xpg4/bin/sh
The getusershell() function opens the file /etc/shells, if it exists, and returns the next entry in the list of shells.
The setusershell() function rewinds the file or the list.
The endusershell() function closes the file, frees any memory used by getusershell() and setusershell(), and rewinds the file /etc/shells.
RETURN VALUES
The getusershell() function returns a null pointer on EOF.
BUGS
All information is contained in memory that may be freed with a call to endusershell(), so it must be copied if it is to be saved.
SunOS 5.10 30 Aug 2004 getusershell(3C)