bash 比较 Unix 中两个文本文件的比较函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12736013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:47:28  来源:igfitidea点击:

Comparison function that compares two text files in Unix

bashunixcmp

提问by Masterminder

I was wondering if anyone could tell me if there is a function available in unix, bash that compares all of the lines of the files. If they are different it should output true/false, or -1,0,1. I know these cmp functions exist in other languages. I have been looking around the man pages but have been unsuccessful. If it is not available, could someone help me come up with an alternative solution?

我想知道是否有人可以告诉我 unix 中是否有一个函数可用,bash 可以比较文件的所有行。如果它们不同,则应输出 true/false 或 -1,0,1。我知道这些 cmp 函数存在于其他语言中。我一直在查看手册页,但没有成功。如果它不可用,有人可以帮我想出替代解决方案吗?

Thanks

谢谢

回答by David W.

There are several ways to do this:

做这件事有很多种方法:

  • cmp -s file1 file2: Look at the value of $?. Zero if both files match or non-zero otherwise.
  • diff file1 file2 > /dev/null: Some forms of the diffcommand can take a parameter that tells it not to output anything. However, most don't. After all, you use diffto see the differences between two files. Again, the exit code (you can check the value of $?will be 0 if the files match and non-zero otherwise.
  • cmp -s file1 file2: 看值$?。如果两个文件匹配,则为零,否则为非零。
  • diff file1 file2 > /dev/null: 某些形式的diff命令可以接受一个参数,告诉它不输出任何内容。然而,大多数没有。毕竟,您习惯于diff查看两个文件之间的差异。同样,退出代码($?如果文件匹配,您可以检查的值为0,否则为非零。

You can use these command in a shell if statement:

您可以在 shell if 语句中使用这些命令:

if cmp -s file1 file2
then
   echo "The files match"
else
   echo "The files are different"
fi

The diffcommand is made specifically for text files. The cmpcommand should work with all binary files too.

diff命令专门用于文本文件。该cmp命令也应该适用于所有二进制文件。

回答by Janito Vaqueiro Ferreira Filho

There is a simple cmp file filecommand that does just that. It returns 0 if they are equal and 1 if they are different, so it's trivial to use in ifs:

有一个简单的cmp file file命令可以做到这一点。如果它们相等则返回 0,如果它们不同则返回 1,因此在ifs 中使用很简单:

if cmp file1 file1; then
    ...
fi

Hope this helps =)

希望这会有所帮助 =)

回答by kris

#!/bin/bash

file1=old.txt
file2=new.txt

echo " TEST 1 : "
echo

if [ $( cmp -s ${file1} ${file2}) ]
then
   echo "The files match :  ${file1} - ${file2}"
else
   echo "The files are different :  ${file1} - ${file2}"
fi

echo
echo " TEST 2 : "
echo
bool=$(cmp -s "$file1" "$file2" )
if cmp -s "$file1" "$file2"
then
   echo "The files match"
else
   echo "The files are different"
fi

echo
echo " TEST 3 : md5 / md5sum - compute and check MD5 message digest"
echo

md1=$(md5 ${file1});
md2=$(md5 ${file2});

mdd1=$(echo $md1 | awk '{print }' ) 
mdd2=$(echo $md2 | awk '{print }' ) 

# or md5sum depends on your linux flavour :D
#md1=$(md5sum ${file1});
#md2=$(md5sum ${file2});

#mdd1=$(echo $md1 | awk '{print }' ) 
#mdd2=$(echo $md2 | awk '{print }' ) 

echo $md1
echo $mdd1
echo $md2
echo $mdd2
echo

#if [ $mdd1 = $mdd2 ]; 
if [ $mdd1 -eq $mdd2 ]; 
then
   echo "The files match :  ${file1} - ${file2}"
else
   echo "The files are different :  ${file1} - ${file2}"
fi

回答by sovick konar

echo "read first file" read f1 echo "read second file" read f2

echo "读取第一个文件" 读取 f1 echo "读取第二个文件" 读取 f2

diff -s f1 f2 # prints if both files are identical

diff -s f1 f2 # 打印两个文件是否相同

回答by Jeremy J Starcher

You could do an md5on the two files, then compare the results in bash.

您可以对两个文件执行md5,然后比较bash.

No Unix box here to test, but this should be right.

这里没有 Unix box 来测试,但这应该是正确的。

#!/bin/bash

md1=$(md5 file1);
md2=$(md5 file2);

if [ $md1 -eq $ $md2 ]; then
  echo The same
else
  echo Different
fi