Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何区分线的各个部分?_Shell - Fatal编程技术网

Shell 如何区分线的各个部分?

Shell 如何区分线的各个部分?,shell,Shell,我有两个需要区分的文件。行中有时间戳,可能还有一些我想在匹配算法中忽略的东西,但是如果匹配算法发现文本的其余部分存在差异,我仍然希望输出这些项目。例如: 1c1 < [junit4] 2013-01-11 04:43:57,392 INFO com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundEx

我有两个需要区分的文件。行中有时间戳,可能还有一些我想在匹配算法中忽略的东西,但是如果匹配算法发现文本的其余部分存在差异,我仍然希望输出这些项目。例如:

1c1
<    [junit4] 2013-01-11 04:43:57,392 INFO  com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
---
>    [junit4] 2013-01-11 22:16:07,398 INFO  com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
1c1
<[junit4]2013-01-11 04:43:57392 INFO com.example.MyClass:123[main][loadOverridePropFile]配置文件application.properties未找到:java.io.FileNotFoundException:/path/to/application.properties(无此类文件或目录)
---
>[junit4]2013-01-11 22:16:07398 INFO com.example.MyClass:123[main][loadOverridePropFile]配置文件application.properties未找到:java.io.FileNotFoundException:/path/to/application.properties(没有此类文件或目录)
不应排放,但:

1c1
<    [junit4] 2013-01-11 04:43:57,392 INFO  com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
---
>    [junit4] 2013-01-11 22:16:07,398 INFO  com.example.MyClass:456 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
1c1
<[junit4]2013-01-11 04:43:57392 INFO com.example.MyClass:123[main][loadOverridePropFile]配置文件application.properties未找到:java.io.FileNotFoundException:/path/to/application.properties(无此类文件或目录)
---
>[junit4]2013-01-11 22:16:07398 INFO com.example.MyClass:456[main][loadOverridePropFile]配置文件application.properties未找到:java.io.FileNotFoundException:/path/to/application.properties(无此类文件或目录)
应发出(因为行号不同)。请注意,时间戳仍然发出


如何做到这一点?

假设您的文件是“a.txt”和“b.txt”。 您可以通过以下方式使用diff+cut获得它:

diff <(cut -d" " -f4-99 a.txt) <(cut -d" " -f4-99 b.txt)
但它不适合我,所以我添加了-f4-99。
因此,我们对两个输入应用cut来忽略日期字段,然后运行diff来比较它们。我之前几次希望使用这个功能,因为它再次出现在这里,所以我决定搜索一下,找到perl的
算法::diff
,您可以为它提供一个哈希函数(他们称之为“密钥生成函数”),该函数“应返回唯一标识给定元素的字符串”,算法用于进行比较(而不是提供给它的实际内容)

基本上,您所需要做的就是添加一个sub,该sub执行一些正则表达式魔术,您希望从字符串中过滤出不需要的内容,并将subref作为参数添加到调用
diff()
(请参见下面代码段中我的
change1
change2
注释)

如果您需要正常(或统一)的
diff
输出,请检查模块附带的详细的
diffnew.pl
示例,并在此文件中进行必要的更改。出于演示目的,我将使用它附带的简单
diff.pl
,因为它很短,我可以完全发布在这里

mydiff.pl 2.txt 示例运行 示例运行2 这里是一个基于
diffnew.pl

$ ./my_diffnew.pl one.txt two.txt
2c2
< 13:21 three four five
---
> 13:21 seven six eight
$。/my_diffnew.pl one.txt two.txt
2c2
<13:21三四五
---
>13:21七六八
正如您所看到的,两个文件中的第一行都会被忽略,因为它们只在时间戳上不同,而哈希函数会删除用于比较的时间戳


瞧,你刚刚推出了自己的内容感知
diff

实现这一点的一种方法是去掉你不想匹配的信息,并仅使用相关数据制作文件的截断副本。不过,你必须保留准确的行号。然后你使用行号返回到你需要的文件ant并获取完整的相关行。这不是我想要的。我仍然希望输出时间戳;它们不应该被标记为不同。
#!/usr/bin/perl

# based on diff.pl that ships with Algorithm::Diff
# demonstrates the use of a key generation function

# the original diff.pl is:
# Copyright 1998 M-J. Dominus. (mjd-perl-diff@plover.com)
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

use Algorithm::Diff qw(diff);

die("Usage: $0 file1 file2") unless @ARGV == 2;

my ($file1, $file2) = @ARGV;

-f $file1 or die("$file1: not a regular file");
-f $file2 or die("$file2: not a regular file");
-T $file1 or die("$file1: binary file");
-T $file2 or die("$file2: binary file");

open (F1, $file1) or die("Couldn't open $file1: $!");
open (F2, $file2) or die("Couldn't open $file2: $!");
chomp(@f1 = <F1>);
close F1;
chomp(@f2 = <F2>);
close F2;

# CHANGE 1
# $diffs = diff(\@f1, \@f2);
$diffs = diff(\@f1, \@f2, \&keyfunc);

exit 0 unless @$diffs;

foreach $chunk (@$diffs)
{
        foreach $line (@$chunk)
        {
                my ($sign, $lineno, $text) = @$line;
                printf "%4d$sign %s\n", $lineno+1, $text;
        }
}
exit 1;

# CHANGE 2 {
sub keyfunc
{
        my $_ = shift;
        s/^(\d{2}:\d{2})\s+//;
        return $_;
}
# }
12:15 one two three
13:21 three four five
10:01 one two three
14:38 seven six eight
$ ./mydiff.pl one.txt two.txt
   2- 13:21 three four five
   2+ 13:21 seven six eight
$ ./my_diffnew.pl one.txt two.txt
2c2
< 13:21 three four five
---
> 13:21 seven six eight