可以使用grep命令来比较两个文件并找到相同的行。然后使用sort命令对结果进行排序并使用uniq命令计算每个唯一行的数量。最后,将结果存储在一个文件中,并在每个行的开头添加IP地址。
代码示例:
#!/bin/bash
file1="file1.txt" file2="file2.txt"
output_file="output.txt"
ip_address=$(hostname -I | cut -d' ' -f1)
duplicates=$(grep -Ff "$file1" "$file2" | sort | uniq -c)
echo "$duplicates" | while read count line; do echo "$ip_address,$count,$line" >> "$output_file" done
echo "Duplicates found and saved in $output_file"