找回密码
 注册

QQ登录

只需一步,快速开始

查看: 436|回复: 0

Bash Read Comma Separated CVS File

[复制链接]
发表于 2011-8-23 09:30:45 | 显示全部楼层 |阅读模式
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.
  1. #!/bin/bash
  2. INPUT=data.cvs
  3. OLDIFS=$IFS
  4. IFS=,
  5. [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
  6. while read flname dob ssn tel status
  7. do
  8.         echo "Name : $flname"
  9.         echo "DOB : $dob"
  10.         echo "SSN : $ssn"
  11.         echo "Telephone : $tel"
  12.         echo "Status : $status"
  13. done < $INPUT
  14. IFS=$OLDIFS
复制代码

script that worked for me
  1. #!/bin/bash
  2. INPUT=./Zabbix_template_item.csv
  3. OLDIFS=$IFS
  4. IFS=";"
  5. [ ! -f $INPUT ] &while read Host Group itItemType itKey itValueType itDescription itDelay itHistory itTrends itDataType itUnits itMultiplier itDelta itFormula itParams itSnmpCom itSnmpOid itApplication
  6. do
  7.         echo "Host : $Host"
  8.         echo "Group : $Group"
  9.         echo "itItemType : $itItemType"
  10.         echo "itKey : $itKey"
  11.         echo "itValueType : $itValueType"
  12.         echo "itDescription : $itDescription"
  13.         echo "itDelay : $itDelay"
  14.         echo "itHistory : $itHistory"
  15.         echo "itTrends : $itTrends"
  16.         echo "itDataType : $itDataType"
  17.         echo "itUnits : $itUnits"
  18.         echo "itMultiplier : $itMultiplier"
  19.         echo "itDelta : $itDelta"
  20.         echo "itFormula : $itFormula"
  21.         echo "itParams : $itParams"
  22.         echo "itSnmpCom : $itSnmpCom"
  23.         echo "itSnmpOid : $itSnmpOid"
  24.         echo "itApplication : $itApplication"

  25. done < $INPUT
  26. IFS=$OLDIFS
复制代码



http://www.cyberciti.biz/faq/unix-linux-bash-read-comma-separated-cvsfile/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-15 14:25 , Processed in 0.016374 second(s), 15 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表