1

我正在使用这个命令

cat text.csv | while read a ; do grep $a text1.csv >> text2.csv; done

text.csv 具有带有完整路径的文件名。文件名有空格。

示例:C:\Users\Downloads\File Name.txt

text1.csv 包含显示用户 ID 和带有完整路径的文件名的日志。

示例:MyName,C:\Users\Downloads\File Name.txt

当我运行命令时,我得到了错误

grep: Name: No such file or Directory

我知道错误是因为文件名中的空格。我想知道如何消除此错误。

4

2 回答 2

5

使用带双引号的 grep 模式,否则 shell 会将其视为 grep 的不同参数:

while read a ; do grep "$a" text1.csv >> text2.csv; done < text.csv

不需要额外的,cat因此我在答案中更改了它。

于 2013-10-21T17:52:49.920 回答
2

引用变量:

cat text.csv | while read a ; do grep "$a" text1.csv >> text2.csv; done

通常,您通常应该引用变量,除非您特别希望该值进行分词和通配符扩展。

于 2013-10-21T17:52:42.883 回答