使用bash脚本不起作用比较两个文件

发布于 2025-01-24 10:04:58 字数 460 浏览 0 评论 0原文

我试图比较两个文件以检查一个文件的内容是否在另一个文件中。 下面如果使用代码段, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聊慰 2025-01-31 10:04:58

建议尝试gawk脚本:

gawk '
   FNR==NR {file1[NR]=$0; next} # Read all lines file1.csv into array file1
   {print; foundit=match($0,file1[FNR])} # print current line from file2.csv, and test line match corresponding data from file1.csv
   foundit {print "found in line."; next} # when reading file2.csv and mathed. print the match and read next line.
   {print "unmatched. "file1[FNR]} # if got here lines not match. print it.
 ' file1.csv file2.csv

注意File1.csv的数字可以大量匹配。

例如,如果file1.csv中的第1行是11和file2.csv中的第1行是3115它将匹配。

如果您在第二行中完善match()函数,则可以轻松完善匹配项。

Suggsting try gawk script:

gawk '
   FNR==NR {file1[NR]=$0; next} # Read all lines file1.csv into array file1
   {print; foundit=match($0,file1[FNR])} # print current line from file2.csv, and test line match corresponding data from file1.csv
   foundit {print "found in line."; next} # when reading file2.csv and mathed. print the match and read next line.
   {print "unmatched. "file1[FNR]} # if got here lines not match. print it.
 ' file1.csv file2.csv

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 is 3115 it will match.

It easy to refine the match if you refine the match() function in 2nd line.

一笔一画续写前缘 2025-01-31 10:04:58

如果有人在寻找解决方案,问题是读取行<代码> \ r 最后。清理后,grep开始匹配行。

#!/bin/bash -x
input="missing.txt"
orders="detailed.csv"
while IFS= read -r line
do
    echo "$line"
    id="${line/
\r'/}"
    if grep -q "$id" "$orders"; then
      echo "Found..."
    else
      echo "NOT FOUND"
    fi
done < "$input"

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.

#!/bin/bash -x
input="missing.txt"
orders="detailed.csv"
while IFS= read -r line
do
    echo "$line"
    id="${line/
\r'/}"
    if grep -q "$id" "$orders"; then
      echo "Found..."
    else
      echo "NOT FOUND"
    fi
done < "$input"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文