使用bash脚本不起作用比较两个文件
我试图比较两个文件以检查一个文件的内容是否在另一个文件中。 下面如果使用代码段, File1只是数字,File2在一行中具有该ID的详细信息(包括ID)。 但是这个片段不起作用,它总是说“找不到”。我已经阅读了很多帖子,看看我在做错了什么。也尝试添加“ -fxq”标志,但无济于事。如果我执行手册grep“ id” file2.csv
,则有效。 知道发生了什么事吗?诸如运行时间bash变量扩展之类的东西吗?
#!/bin/bash
input="file1.csv"
orders="file2.csv"
while IFS= read -r line
do
echo "$line"
if grep "$line" "$orders"; then
echo "Found..."
else
echo "NOT FOUND"
fi
done < "$input"
I was trying to compare two files to check the content of one file is in the other or not.
Below if the code snippet used,
file1 is just numbers and file2 has the details (including the id) for that id in one line.
But this snippet is not working, it always says 'not found'. I have read through a lot of SO posts to see what I'm doing wrong; tried adding '-Fxq' flags as well, but to no avail. If I do a manual grep "id" file2.csv
, it works.
Any idea what is going on? Something like run time bash variable expansion required?
#!/bin/bash
input="file1.csv"
orders="file2.csv"
while IFS= read -r line
do
echo "$line"
if grep "$line" "$orders"; then
echo "Found..."
else
echo "NOT FOUND"
fi
done < "$input"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议尝试
gawk
脚本:注意File1.csv的数字可以大量匹配。
例如,如果file1.csv中的第1行是
11
和file2.csv中的第1行是3115
它将匹配。如果您在第二行中完善
match()
函数,则可以轻松完善匹配项。Suggsting try
gawk
script:Notice the numbers from file1.csv could be matched as sub-strings in large numbers.
For example if line #1 in file1.csv is
11
and line #1 in file2.csv is3115
it will match.It easy to refine the match if you refine the
match()
function in 2nd line.如果有人在寻找解决方案,问题是读取行<代码> \ r 最后。清理后,
grep
开始匹配行。In case anyone looking for the solution, The problem was the line read has
\r
at the end. Once it was cleaned up,grep
started matching lines.