繁体   English   中英

在shell脚本中从csv文件读取

[英]reading from csv file in shell scripting

我正在运行脚本run.sh。 该脚本执行如下。 $。/ run.sh read.csv脚本的内容如下。

   tail -n +2 $1 | while IFS="," read -r A B C D E F;
    do
        python test.py ${A} ${B} ${C} ${D} ${E} ${F}
    done

我的问题是“如果我需要像这样从终端传递附加的命令行参数以及read.csv(例如: $./run.sh name sex DOB read.csv ),如何修改代码,以便它工作正常。

因为如果我将任何其他命令行参数与文件名(read.csv)一起传递,则会收到对文件read.csv的访问错误

位置参数就是您所追求的。 这是您可以执行的操作:

tail -n +2 $4 | while IFS="," read -r A B C D E F;##note now you would pass $4 to tail command which is your file name
do
    python test.py ${A} ${B} ${C} ${D} ${E} ${F}
done

您可以访问以下值,例如$ 1中的name,$ 2中的sex, $2中的DOB和$ 4中的read.csv。

暂无
暂无

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

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