2017-09-15 62 views
1

我尝试到grep从内容“test.csv”内的一切对book.1grep的存储在一个CSV文件

while IFS= read -r result 
do 
grep -r $result test.csv >> output.csv 

echo $result 

done<book1.csv 

我的书1具有1行数据是列A 。

我得到的屏幕所以它是正确分析上显示book1.csv的结果,但我越来越没有在output.csv

我已经手动检查grep的&有内容book1.csv内test.csv

我做错了什么?

编辑

样本输入book1.csv

HOSTNAME 
TEST1 
TEST2 

样品输入test.csv

blank,TEST1.abc.123,blank,date 

我想从book1.csv每一行是test.csv

Cat -a book1 result 

TEST1^M$ 
+0

当我这样做的“grep -r TEST1 test.csv >> output.csv”没有剧本,我得到预期的数据,所以它的东西与我的$办结果变量 – Bunion

+1

'grep -Ff book1.csv test.csv'通常可以工作,但您也必须提供'test.csv'的样本数据。还要检查'cat -A book1.csv'和'cat -A test.csv'的输出以确保您没有DOS回车符。 – anubhava

+0

添加样本输入,当我做了上面的grep -Ff我什么也没有得到。但是,如果我做grep TEST1测试。csv我得到的结果,所以我不知道什么是错的 – Bunion

回答

2

as suspecte d最初您的.csv文件的DOS行以\r个字符结尾,这导致grep -f不匹配,因为Unix文件刚刚以\n结尾。

您可以使用此命令tr跑跑grep之前将来自两个.csv文件\r

grep -Ff <(tr -d '\r' < book1.csv) <(tr -d '\r' < test.csv) 

blank,TEST1.abc.123,blank,date 

tr -d '\r' < book1.csv将从给定的文件中删除\r或回车符。

1

使用fgrep应该工作。根据您提供的数据,它提供了:

fgrep -f book1.csv test.csv 

输出:

blank,TEST1.abc.123,blank,date 

fgrep是grep的手册页完全等同于grep -F

Matcher Selection 
    -F, --fixed-strings 
      Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) 


Matching Control 
    -f FILE, --file=FILE 
      Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified 
      by POSIX.) 

尖的out by anubhava,你需要删除DOS字符。要做到这一点,只需使用dos2unix命令:

fgrep -f <(dos2unix book1.csv) <(dos2unix test.csv) 
+0

这也工作谢谢! – Bunion

相关问题