假设我有这个文件:
=
我想匹配=
左边的行与AND 1=1
AND fejo = fejo
符号右边的行匹配,因此,它必须与以下行匹配:
if(uniqueId == null) {
uniqueId = UUID.randomUUID().toString();
}
答案 0 :(得分:2)
grep -E '^AND\s+([^=\s]*)\s*=\s*\1\b'
可以正常输入。
^ # begin of line (preg tries to match the regex against each line)
AND # match literal 'AND'
\s+ # match one or more whitespace characters
( # beginning of a group
[ # beginning of a character class that...
^ # ... match any character that is not listed here:
= # literal '='
\s # whitespace
] # end of the character class...
# ... that matches one character that is not '=' or whitespace
* # zero or more occurrences of the previous expression (the class)
) # end of the capturing group
\s* # match zero or more spaces...
= # the '=' character
\s* # ... around the equal sign
\1 # match the text captured by the first (and only) group above
\b # match a word boundary, to make sure \1 is not just a prefix of a longer word
上面的regex
仅匹配以大写AND
开头的行。如果您还需要匹配以and
(小写)开头的行或这些字符的其他大写/小写组合,则可以使用AND
替换regex
中的[aA][nN][dD]
。
将-i
添加到grep
命令行会使其忽略regex
和输入中的大小写。 regex
将匹配and 1 = 1
,但也会and fejo = FEJO
,这可能不是您所需要的。
答案 1 :(得分:0)
使用awk:
$ awk 'split($0,a," *= *") && a[1]==($1 " " a[2])' file
AND 1=1
AND fejo = fejo
split
将=
上的记录拆分为AND 1
和1
,在$1
前面添加AND
即a[2]
} {ie 1
并进行比较。如果在$1
之后有多个空格,则会失败。为避免这种情况,这似乎也有效:
$ awk 'split($0,a,"( *= *| *)") && a[2]==a[3]' file
AND 1=1
AND fejo = fejo
缺点是被比较的元素不能有空间。这个清除了第一个单词及其周围空间śplit
s =
(包括周围空间)并比较了半个。
$ awk ' {
r=$0 # working copy of record
sub(/^ *[^ ]* */,"",r) # remove AND
n=split(r,a," *= *") # split at =
if((n>1)&&a[1]==a[n]) # if r was really split in half and halfs match
print
}' file
AND 1=1
AND fejo = fejo
答案 2 :(得分:0)
我找到了另一个非常简单的解决方案,而不必弄得一团糟:
AND (\w+)\s*=\s*\1