我想比较两个包含相同内容的文件。下面是我的代码来比较两个文件,但没有正常工作,我不知道错误是什么
AbstractEntity
例如我的文件data.txt包含类似这样的内容
public ActionResult connectDB()
{
const string DB_CONN_STR = "Server=MyServer;Port=MyPort;Uid=MyUid;Database=MyDB;";
MySqlConnection cn = new MySqlConnection(DB_CONN_STR);
try
{
string sqlCmd = "select * from t_documento";
MySqlDataAdapter adr = new MySqlDataAdapter(sqlCmd, cn);
adr.SelectCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
adr.Fill(dt); //opens and closes the DB connection automatically !! (fetches from pool)
return Content(JsonConvert.SerializeObject(dt).ToString());
}
catch (Exception ex)
{
Console.WriteLine("{oops - {0}", ex.Message);
return Content(ex.ToString());
}
finally
{
cn.Dispose(); // return connection to pool
}
}
和我的data1.txt包含类似这样的
open ( FILE , '<', "data.txt" ) or die $!;
open ( FILE1 , '<', "data1.txt" ) or die $!;
my @data = <FILE>;
my @data1 = <FILE1>;
my $match = 0;
my $no_match = 0;
for ( $i =0 ; $i<=864 ; $i++ ) {
my $data_line = $data[$i];
my $data1_line = $data1[$i];
if ( $data1_line eq $data_line ){
$match += 1;
print " total match = $match \n";
}
else {
$no_match +=1 ;
print " line $i no match\n ";
}
}
我也尝试按空格分割并进行比较,但它会使我的代码更长。比较两个文件的最佳方法是什么?我的预期输出是它会显示哪条线不匹配以及总匹配线多少。对于上面的示例我想显示如下结果
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
....
然而,我从上面的脚本获得的输出是所有行不匹配。感谢
答案 0 :(得分:0)
!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# read the data
my ($file1, $file2) = ('file1.txt', 'file2.txt');
my @firstfile;
my @secondfile;
open (my $fh, '<', $file1) or die "Cant open $!";
while (my $line = <$fh>) {
chomp $line;
push @firstfile, split / /, $line;
}
open (my $fh2, '<', $file1) or die "Cant open $!";
while (my $line = <$fh2>) {
chomp $line;
push @secondfile, split / /, $line;;
}
# cmp the data in files
my %cmp;
%cmp = map {$_ => 1} @firstfile;
foreach my $element (@secondfile) {
if ($cmp{$element}) {
print "$element exists in both files\n";
} else {
print "$element only in second file\n";
}
}
答案 1 :(得分:-1)
这是一个快速shell解决方案,用于检查这两个文件是否具有完全相同的内容。如果以下脚本的结果为零,则两个文件具有相同的内容。
diff data.txt data1.txt | wc -l
更具体地说:
diffLines=$( diff data.txt data1.txt | wc -l )
if [ $diffLines -eq 0 ]
then
echo "Files have the same content"
fi