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 DEBIAN
linux::distribution::packages
Linux::Distribution::Packages(3pm) User Contributed Perl Documentation Linux::Distribution::Packages(3pm)NAME
Linux::Distribution::Packages - list all packages on various Linux distributions
SYNOPSIS
use Linux::Distribution::Packages qw(distribution_packages distribution_write);
$linux = new Linux::Distribution::Packages({'format' => 'csv', 'output_file' => 'packages.csv'});
$linux->distribution_write();
# Or you can (re)set the options when you write.
$linux->distribution_write({'format' => 'xml', 'output_file' => 'packages.xml'});
# If you want to reload the package data
$linux->distribution_packages();
DESCRIPTION
This is a simple module that uses Linux::Distribution to guess the linux distribution and then uses the correct commands to list all the
packages on the system and then output them in one of three formats: native, csv, and xml.
Distributions currently working: debian, ubuntu, fedora, redhat, suse, gentoo, slackware, redflag.
The module inherits from Linux::Distribution, so can also use its calls.
EXPORT
None by default.
TODO
* Add the capability to correctly get packages for all recognized distributions. * Seperate out parsing from writing. Parse data to hash
and give access to hash. Then write the formatted data from the hash.
AUTHORS
Judith Lebzelter, <judith@osdl.org> Alberto Re, <alberto@accidia.net>
COPYRIGHT AND LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.
perl v5.10.1 2006-04-19 Linux::Distribution::Packages(3pm)