0

我有两个文件

文件 1

INFO  : temp0 Directory
            created
INFO  : temp0 Directory created

文件2

INFO  : reuse temp1

因此,在区分这两个文件时,我想忽略以Directory created. 我写

diff -I 'Directory created' file1 file2

所以这个语句成功地忽略了 file1 中的第二行,但它没有忽略包含Directory created在单独行中的第一行。

因此,如果有人知道解决方案,请帮助我!

4

2 回答 2

0

基本上 -I仅在差异块中的所有行都匹配的情况下才会匹配。在您描述的情况下 - 条件不成立。

diff -I "INFO" 1 2 | grep -v "Directory created"

可能对你有用,但它不能正确计算差异块边界

希望有帮助

于 2012-08-24T08:43:43.120 回答
0

好吧,我认为最简单的解决方案是从第一个文件中过滤掉这些行。例如,您可以使用以下 awk 脚本执行此操作:

# if line contains 'Directory created', just skip it.
/Directory created/ {
    had_dir = ""
    next
}

# if line contains sole 'Directory', store it and lookup next line.
/Directory/ {
    had_dir = $0
    next
}

# if line contains sole 'created' and we stored a line (which means
# it contained 'Directory'), then delete the stored one and skip
# (effectively, skip both).
/created/ && had_dir {
    had_dir = ""
    next
}

# otherwise, if we stored a line, print it.
had_dir {
    print had_dir
    had_dir = ""
}

# and print the current line.
{
    print
}

如果您的输入更复杂,您可能需要稍微调整一下正则表达式。

然后,使用脚本过滤掉第一个文件:

$ awk -f script.awk file1 | diff - file2
0a1
> INFO  : reuse temp1

请注意,您将-作为第一个参数传递给diff, 以使其读取管道输出awk而不是文件。

还要注意,线偏移将不再正确。

于 2012-08-24T10:24:22.120 回答