|
|
read comma separated CVS file under UNIX / Linux / BSD / Mac OS X bash script. sample file is as follows:
FirstName LastName,DOB,SSN,Telephone,Status
can use while shell loop to read comma-separated cvs file. IFS variable will set cvs separated to , (comma). read command will read each line and store data into each field.
- #!/bin/bash
- INPUT=data.cvs
- OLDIFS=$IFS
- IFS=,
- [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
- while read flname dob ssn tel status
- do
- echo "Name : $flname"
- echo "DOB : $dob"
- echo "SSN : $ssn"
- echo "Telephone : $tel"
- echo "Status : $status"
- done < $INPUT
- IFS=$OLDIFS
复制代码
script that worked for me
- #!/bin/bash
- INPUT=./Zabbix_template_item.csv
- OLDIFS=$IFS
- IFS=";"
- [ ! -f $INPUT ] &while read Host Group itItemType itKey itValueType itDescription itDelay itHistory itTrends itDataType itUnits itMultiplier itDelta itFormula itParams itSnmpCom itSnmpOid itApplication
- do
- echo "Host : $Host"
- echo "Group : $Group"
- echo "itItemType : $itItemType"
- echo "itKey : $itKey"
- echo "itValueType : $itValueType"
- echo "itDescription : $itDescription"
- echo "itDelay : $itDelay"
- echo "itHistory : $itHistory"
- echo "itTrends : $itTrends"
- echo "itDataType : $itDataType"
- echo "itUnits : $itUnits"
- echo "itMultiplier : $itMultiplier"
- echo "itDelta : $itDelta"
- echo "itFormula : $itFormula"
- echo "itParams : $itParams"
- echo "itSnmpCom : $itSnmpCom"
- echo "itSnmpOid : $itSnmpOid"
- echo "itApplication : $itApplication"
- done < $INPUT
- IFS=$OLDIFS
复制代码
http://www.cyberciti.biz/faq/unix-linux-bash-read-comma-separated-cvsfile/
|
|