在Unix的比较两个文本文件的比较功能 [英] Comparison function that compares two text files in Unix

查看:205
本文介绍了在Unix的比较两个文本文件的比较功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人能告诉我,如果有在UNIX中可用的一项功能,即bash的比较所有文件的线条。如果它们不同,它应该输出真/假,或-1,0,1。我知道,在其他语言中存在这些CMP功能。我一直在四处寻找该名男子的网页,但没有成功。如果没有,可能有人帮我想出了一个替代解决方案?

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?

感谢

推荐答案

有几种方法可以做到这一点:

There are several ways to do this:


  • CMP -s $ $文件1文件2 :看 $ 的价值?零,如果两个文件匹配,或不为零,否则。

  • 差异$ $的file1 file2的>的/ dev / null的:在差异命令的某些形式可以把它告诉不输出任何一个参数。然而,大多数没有。毕竟,你使用差异来看到两个文件之间的差异。再次,退出code(你可以检查 $?的值为0,如果这些文件匹配,否则返回非零值。

  • 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 diff command can take a parameter that tells it not to output anything. However, most don't. After all, you use diff to 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.

您可以在shell中使用这些命令,如果语句:

You can use these command in a shell if statement:

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

差异命令对文本文件专门提出。在 CMP 命令应与所有二进制文件也可以工作。

The diff command is made specifically for text files. The cmp command should work with all binary files too.

这篇关于在Unix的比较两个文本文件的比较功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆