awk - 读取文件

时间:2016-07-01 04:33:10

标签: shell unix

我遇到与awk命令相关的问题。

根据以下功能,Input-command产生5个csv文件&将它们转储到相应的csvFolder中。问题是当调用函数时它会抛出错误,因为awk没有得到$I的值。

  

awk:致命:......阅读(没有这样的文件或目录)

如果csvFolder或csvFolder2中没有csv文件,有没有办法绕过这个awk条件?

Function() {

#CSV prducer command

Input-command="$command-to-calculate"

#Parse csv files to get row count which do not have special-word

for I in csvFolder/*.csv
Do
   Var=$(awk 'NR>1' csvFolder/$I | grep -cv 'special-word')
   Echo $Var
Done 

#CSV prducer command no. 2

Input-command-2="$command-to-calculate" 

#Parse csv files to get row count which do not have special-word

for I in csvFolder2/*.csv
Do 
   Var=$(awk 'NR>1' csvFolder2/$I | grep -cv 'special-word')
   Echo $Var
Done 

}

1 个答案:

答案 0 :(得分:0)

使用当前的for循环,即:

for I in csvFolder/*.csv

I的值为csvFolder/sample.csv。所以你不需要做

Var=$(awk 'NR>1' csvFolder/$I | grep -cv 'special-word')

取而代之的是

Var=$(awk 'NR>1' "$I" | grep -cv 'special-word')

此外,grep在这里是多余的。如果你试图计算不包含'特殊字'的行,你可以这样做:

Var=$(awk 'BEGIN{FS=","}!/special-word/{if(NR>1){count++}}END{print count}' "$I")

旁注:

外壳区分大小写,Echo $Var应为echo $Var,依此类推。