我需要与执行不区分大小写的匹配格雷普迪夫工具。与 grep 不同,它没有参数,但它有--extended-regexp
.是否可以?
我尝试了perl之类的,但它不起作用
$ echo SOME | egrep '(?i)some'
$ echo SOME | grepdiff --extended-regexp '(?i)some'
grepdiff: repetition-operator operand invalid
答案1
你需要告诉grep
家人使用类似 Perl 的 RE。
echo SOME | grep -P '(?i)some'
SOME
不幸的是,这个标志不可用grepdiff
,而且据我所知,也不可用-i
(不区分大小写的匹配)。
这是一个小脚本,我称之为pgrepdiff
,它允许您使用 PCRE 来匹配您的diff
输出。它不接受任何grepdiff
允许的标志。它从命名文件中读取或标准输入如果没有提供。省略 RE 会使其成为一个昂贵的cat
运算符:
#!/bin/bash
#
# Usage: pgrepdiff <re> [<files...>]
#
# https://unix.stackexchange.com/a/424566/100397
#
tmpd=$(mktemp --tmpdir --directory 'pgd.XXXXXXXXXX')
re="$1"
shift
# Split the file into its context diff parts
cat "$@" | csplit - -f "$tmpd/xx." -k -n4 -s -z '/^@@/' '{*}'
# Save the header preamble for a match
mv -f "$tmpd/xx.0000" "$tmpd/head"
head=
# Iterate across all the diff segments
shopt -s nullglob
for xx in "$tmpd"/xx.????
do
if grep -q -P "$re" "$xx"
then
[[ -z $head ]] && cat "$tmpd/head" && head=yes
cat "$xx"
fi
done
# Tidy up
rm -rf "$tmpd"
# Return 0 (success) iff we matched something
[[ -n $head ]]