如何从差异比较中查找和排除字符串正则表达式文字?

时间:2022-05-22 00:11:11

I'm trying to write a linux diff statement to ignore all line differences that have a literal string somewhere in them. (In my case I want to ignore lines with url differences.)

我正在尝试编写一个linux diff语句来忽略在其中某处有文字字符串的所有行差异。 (在我的情况下,我想忽略带有url差异的行。)

Currently I've tried

目前我已经尝试过了

diff -I '^insert.*' file1.txt file2.txt > outputDiff.txt

The output here took out lines that started with insert, so that's a step in the right direction, but I need to remove a longer string literal (url) with lots of periods and slashes.

这里的输出取出了以insert开头的行,所以这是向正确方向迈出的一步,但我需要删除一个带有大量句点和斜杠的较长字符串文字(url)。

To be exact: I need to look for lines that have:

确切地说:我需要查找具有以下内容的行:

ta.com/nmo/nmdr/templates/nmdr.css\

somewhere in them, and exclude them from the diff result.

在它们的某个地方,并将它们从diff结果中排除。

How do I look for a literal string with a regex that will work within a diff -I statement? (Include the start of the line and the end of the line if it's needed for my diff statement)

如何查找带有可在diff -I语句中使用的正则表达式的文字字符串? (如果我的diff语句需要,则包括行的开头和行的结尾)

2 个解决方案

#1


0  

You need to escape . and \ in the regexp.

你需要逃脱。和\在正则表达式。

diff -I 'ta\.com/nmo/nmdr/templates/nmdr\.css\\' file1.txt file2.txt > outputDiff.txxdt

#2


0  

In one command:

在一个命令中:

diff -I 'ta\.com/nmo/nmdr/templates/nmdr\.css\\' file1.txt file2.txt \
  > outputDiff.txt

This is escaping the special . character (which usually means "any character"), and also handling the literal backslash.

这是特殊的逃避。字符(通常表示“任何字符”),也处理字面反斜杠。

Or, you could do it (slightly less efficiently) in a chained command:

或者,您可以在链式命令中执行此操作(效率稍低):

diff file1.txt file2.txt | fgrep -v 'ta.com/nmo/nmdr/templates/nmdr.css\\' \
  > outputDiff.txt

fgrep, or equivalently grep -F, stands for "fast grep" and automatically escapes special characters like .

fgrep,或等效grep -F,代表“快速grep”并自动转义特殊字符,如。

#1


0  

You need to escape . and \ in the regexp.

你需要逃脱。和\在正则表达式。

diff -I 'ta\.com/nmo/nmdr/templates/nmdr\.css\\' file1.txt file2.txt > outputDiff.txxdt

#2


0  

In one command:

在一个命令中:

diff -I 'ta\.com/nmo/nmdr/templates/nmdr\.css\\' file1.txt file2.txt \
  > outputDiff.txt

This is escaping the special . character (which usually means "any character"), and also handling the literal backslash.

这是特殊的逃避。字符(通常表示“任何字符”),也处理字面反斜杠。

Or, you could do it (slightly less efficiently) in a chained command:

或者,您可以在链式命令中执行此操作(效率稍低):

diff file1.txt file2.txt | fgrep -v 'ta.com/nmo/nmdr/templates/nmdr.css\\' \
  > outputDiff.txt

fgrep, or equivalently grep -F, stands for "fast grep" and automatically escapes special characters like .

fgrep,或等效grep -F,代表“快速grep”并自动转义特殊字符,如。