我需要逐个阅读一些 CSV 文件, CSV 文件将命名为 Test1.csv , Test2 .csv 等代码:
#!/bin/bash
IFS=","
FILES=/CSVFiles/*
CSVNAME=Test
n=1
for f in $FILES
while read Column1 Column2
do
echo $Column1
echo $Column2
done < "$CSVNAME"$n.csv
n=$((n+1))
返回这些错误:
./ ReadCSV.sh:line 23:意外令牌附近的语法错误`while&#39;
./ ReadCSV.sh:line 23:`读取Column1 Column2
答案 0 :(得分:0)
你错过了
do
后
for f in $FILES
但如果我理解你的意图,你可以使用下面的代码改进这个例子:
#!/bin/bash
for file in /CSVFiles/Test*.csv # Use file globbing to get the files
do
/*Use a stream editor like awk to print the lines*/
awk -v FS="," '{
/* if you want the columns in separate lines */
print $1;
print $2;
}' "$file"
done
编辑2:如果您需要考虑文件顺序,下面的代码可以帮助
#!/bin/bash
#Count the total number of files using stat and grep
total=$(stat -f /CSVFiles/Test*.csv | grep -c '^[[:blank:]]*File:')
#User c-style for-loops in bash
for((i=0;i<="$total";i++))
do
awk -v FS="," '{
/* if you want the columns in separate lines */
print $1;
print $2;
}' "Test${i}.csv" # Feeding the file names in order
done
答案 1 :(得分:0)
这个怎么样。
IFS=","
FILES=/CSVFiles/*
cat $FILES|while read Column1 Column2
do
echo $Column1
echo $Column2
done
答案 2 :(得分:0)
OP脚本的输出似乎等同于这个单行:
cut -d, -f1,2 /CSVFiles/Test*.csv | tr , '\n'