【问题标题】:Reading a string from one file and comparing it with another file and giving alarm if condition met从一个文件中读取一个字符串并将其与另一个文件进行比较,如果条件满足则发出警报
【发布时间】:2022-01-11 21:14:04
【问题描述】:

我有2 文件File1, and File2。 File1 是strings、line1 contain string1、line2 contains strings2 and so on 的列表。我想从File1 中选择string1 并将其与File 2 进行比较。然后我选择string2 并将其与File2 进行比较。该脚本不知道File1 and Fil2 中有多少个字符串。一旦来自File1 are compared with File2, 的所有字符串,脚本应该给出一个alarm/pop up window,其中strings 列表与File2 匹配。

【问题讨论】:

  • 您是否正在寻找 bash 中的解决方案?或任何编程语言。好吗?
  • satvik choudhary,是的 bash linux。你能指导我吗
  • 它必须是for 循环(或任何循环)吗?因为您要的是一个集合交点,即File1 和File2 中的行。这可以使用单个 grep 或 sort 或其他几个来完成。
  • 到目前为止你尝试了什么?
  • 分而治之。从循环读取File1 开始,然后添加其他内容。

标签: linux string bash shell for-loop


【解决方案1】:

我不明白你所说的弹出/窗口是什么意思。 此外,最好使用 grep 之类的东西。 根据需求,这是我想出来的。

// abc.txt
abcd
ab
cd
abc

// def.txt
dfd
abc
ab
abcd



file1=()
file2=()

filename1="abc.txt"
filename2="def.txt"
while read -r line; do
    name="$line"
    file1+=("$name")
done < "$filename1"

while read -r line; do
    name="$line"
    file2+=("$name")
done < "$filename2"

for i in "${file2[@]}"
do
   for j in "${file1[@]}"
   do
      if [ "$i" = "$j" ]; then
         echo "Match found $i"
         break
      fi
   done
done

【讨论】:

  • @sarvik .about 弹出窗口意味着一旦脚本运行完毕,我应该得到一个带有匹配字符串列表的警报。脚本运行后如何获得警报?
  • 在 Ubuntu 中将 echo "Match found $i" 替换为 notify-send "$i" 会给你很好的桌面通知。不知道你说的报警是这个意思。
  • 您的代码是否在每次匹配后给出所有匹配字符串或打印的列表?
  • 在每场比赛后打印。您可以添加到这样的列表中。 list+=($i) 而不是 echo "Match found $i"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多