[英]Read a csv file from a bash scripting file
in a folder, I have 2 files. 在一个文件夹中,我有2个文件。 Valuation.csv and another file myScript.sh Valuation.csv和另一个文件myScript.sh
My CSV file has 10 lines and 5 columns. 我的CSV文件有10行5列。 I have tried to read it multiple times but it never worked. 我已经尝试过多次阅读,但从未成功。 This is what I have tried as code inside my myScript.sh: 这是我在myScript.sh中尝试作为代码进行的操作:
First try: 第一次尝试:
#!/bin/bash
while read -r line do
field1=$(echo $line | awk -F'|' '{printf "%s", $1}' | tr -d '"')
field2=$(echo $line | awk -F'|' '{printf "%s", $2}' | tr -d '"')
echo $field1 $field2 done < $Valuation.csv
Result: /myScript.sh: line 10: .csv: No such file or directory 结果:/myScript.sh:第10行:.csv:无此类文件或目录
Second try: 第二次尝试:
cat Valuation.csv|while read line do
read -d, col1 col2 < <(echo $line)
echo "I got:$col1|$col2" done
Result: nothing 结果:无
I am running the file like this: 我正在运行这样的文件:
./myScript.sh ./myScript.sh
thank you 谢谢
here is my file data.csv 这是我的文件data.csv
Site,Post,Subject,User,Status
stackover flow,bash,read csv,James,ok
git,linux,core,Novy,ko
in my bash script i have something like: 在我的bash脚本中,我有类似以下内容:
#!/bin/bash
INPUT=./data.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read site post subject usr status
do
echo "Site : $site"
echo "Post : $post"
echo "Subject : $subject"
echo "User : $usr"
echo "Status : $status"
done < $INPUT
IFS=$OLDIFS
Output 产量
Site : stackover flow Post : bash Subject : read csv User : James Status : ok Site : git Post : linux Subject : core User : Novy Status : ko 站点:stackover flow帖子:bash主题:读取csv用户:James状态:ok站点:git帖子:linux主题:core用户:Novy状态:ko
James, avoid calling utilities within a loop in bash. 詹姆斯,避免在bash循环内调用实用程序。 That is horribly inefficient. 那是非常低效的。 Avoid using a fixed number of fields -- what if another is added? 避免使用固定数量的字段-如果添加另一个字段怎么办? You can read .csv
data in bash fairly easily by simply using read -a array...
and setting the internal field separator to word-split on a comma (eg IFS=$',\\n'
). 只需使用read -a array...
并将内部字段分隔符设置为以逗号分隔 (例如IFS=$',\\n'
) IFS=$',\\n'
就可以相当轻松地read -a array...
bash中的.csv
数据。
For example, writing a script that takes the .csv
filename to read as the first argument, you could do: 例如,编写一个将.csv
文件名作为第一个参数读取的脚本,您可以执行以下操作:
#!/bin/bash
declare -i row=0
while IFS=$',\n' read -r -a array; do
printf "row %d - read %d fields.\n" $((row++)) ${#array[@]}
for i in "${array[@]}"; do
printf " %-12s" "$i"
done
echo ""
done < "$1"
( note: while you would in-practice want to handle the entire matter in awk
, since this appears to be a bash exercise, for short .csv
files, bash is more than capable) ( 注意:虽然您可能会想在awk
处理整个问题,但由于这似乎是bash练习,所以对于短的.csv
文件,bash的功能不胜枚举)
Example Data 示例数据
(I wasn't going to retype your spreadsheet, so here are the first two rows) (我不想重新输入您的电子表格,所以这里是前两行)
$ cat company.csv
Facebook,35587,55800000000,1567988,491000000000,8.80x
Uber,16000,11300000000,706250,120000000000,10.6x
Running the script and simply outputting the row count and fields-per-row followed by the field values themselves, you would get: 运行脚本并仅输出行数和每行字段,然后输出字段值本身,您将获得:
$ bash readcsv.sh company.csv
row 0 - read 6 fields.
Facebook 35587 55800000000 1567988 491000000000 8.80x
row 1 - read 6 fields.
Uber 16000 11300000000 706250 120000000000 10.6x
Look things over and let me know if you have questions. 仔细看看,如果您有疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.