简体   繁体   English

CSV Bash循环变量问题

[英]CSV Bash loop Issue with Variables

I have a csv file which im trying to loop through with the purpose to find out if an User Input is found inside the csv data. 我有一个csv文件,我试图通过它循环查找是否在csv数据内找到了用户输入。 I wrote the following code which sometimes works and others doesn't. 我写了下面的代码,有时可能有用,而其他的则无效。 It always stops working when I try to compare to a 2+ digit number. 当我尝试与2位以上的数字进行比较时,它总是会停止工作。 It works OK for numbers 1 through 9, but once u enter lets say 56 , or 99 or 100, it stops working. 1到9的数字可以正常工作,但是一旦输入56或99或100,它就会停止工作。

the csv data is comma delimited, i have about 300 lines they are just like this. csv数据以逗号分隔,我大约有300行,就像这样。

1,John Doe,Calculus I,5.0
1,John Doe,Calculus II,4.3
1,John Doe,Physics II,3.5
2,Mary Poppins,Calculus I,3.7
2,Mary Poppins,Calculus II,4.7
2,Mary Poppins,Physics I,3.7

Data is just like that, all the way down until ID #100 for a total of 300 lines. 数据就是这样,一直到ID#100,总共300行。 Both the sh file and csv file are in the same folder, I'm using a fresh installation of Ubuntu 12.04.3, using gedit as the text editor. sh文件和csv文件都在同一文件夹中,我使用的是Ubuntu 12.04.3全新安装,使用gedit作为文本编辑器。

I tried Echoing the variables ID and inside the IF conditionals but it doesn't behave the way it should when testing for the same value. 我尝试在IF条件条件中回显变量ID和变量,但它的行为与测试相同值时不同。 Could someone point me out in the right direction. 有人可以指出我正确的方向。 Thanks 谢谢

Here's the code: 这是代码:

#s!/bin/bash
echo "enter your user ID";
read user;

INPUT_FILE=notas.csv
while IFS="," read r- ID name asignature final;
do
if [$ID = $user]; then
userType=1;
else
userType=2;
fi
done < notas.csv

You should quote your variables in the if test statement. 您应该在if测试语句中引用变量。 You should also perform a numeric test -eq rather than a string comparison = . 您还应该执行数字test -eq而不是字符串comparison = So your if statement should look like: 因此,您的if语句应类似于:

if [[ "$ID" -eq "$user" ]]

Well, your code as written has a few issues. 好吧,您编写的代码存在一些问题。

  1. You have r- instead of -r on the read line - I assume that's a typo not present in your actual code or you wouldn't get very far. 您在read行上使用r-而不是-r我认为您的实际代码中没有错字,否则您不会走得太远。

  2. Similarly, you need space around the [ ... ] brackets: [$ID is a syntax error. 同样,您需要在[ ... ]方括号内留空格: [$ID是语法错误。

  3. You need to quote the parameter expansions in your if clause, and/or switch bracket types. 您需要在if子句中引用参数扩展,和/或切换括号类型。 You probably make it a numeric comparison as @imp25 suggested, which I would do by using (( ... )) . 您可能按照@ imp25的建议进行了数值比较,我可以使用(( ... ... ))

  4. You probably don't want to set userType to 2 in an else clause, because that will set it to 2 for everyone except whoever is listed last in the file (ID 100, presumably). 您可能不希望在else子句中将userType设置为2,因为对于每个人,除非文件中最后列出的那个人(ID 100,大概是), else将其设置为2。 You want to set it to 2 first, outside the loop. 您想在循环之外首先将其设置为2。 Then, inside the loop when you find a match, set it to 1 and break out of the loop: 然后,在循环中找到匹配项时,将其设置为1并退出循环:

     userType=2 while IFS=, read -r ID name asignature final; do if (( $ID == $user )); then userType=1; break fi done < notas.csv 

You could also just use shell tools like awk: 您也可以只使用诸如awk之类的shell工具:

userType=$(awk -F, -vtype=2 '($1=="'"$user"'") {type=1}; END {print type}' notas.csv)

or grep : grep

grep -q "^$user," notas.csv
userType=$(( $? + 1 ))

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM