找到更多的在代码与egrep换行?(复制) - Find more one newlines in code with egrep? [duplicate]

- 此内容更新于:2015-12-20
主题:

这个问题已经有了一个答案:如何给一个模式在grep新行吗?4的答案如何找到不止一个换行符与正则表达式并添加代码之前警告吗?这就是我试图在xcode中添加bash脚本:

原文:

This question already has an answer here:

How to find more than one newline in code before } with regular expression and add warning to it? This is what I tried add bash script in xcode:

TAGS2="\}/\n"
echo "searching ${SRCROOT} for ${TAGS2}"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) -print0 | xargs -0
egrep --with-filename --line-number --only-matching "($TAGS2).*\$" |
perl -p -e "s/($TAGS2)/ warning\$1/"
网友:如果第一个作品,为什么在你的问题吗?第二个应该做的是什么?没有,我可以看到在任何模式,所以我不确定问题是如何相关的代码。

(原文:If the first one works, why is it in your question? And what is the second one supposed to do? There is no } that I can see in any pattern, so I'm not sure how the question is related to the code.)

网友:你可以包括一个输入和预期输出吗?

(原文:Can you include an input and expected output?)

楼主:@RichardSt-Cyr我想要在我的代码找到超过1换行和突出。

(原文:@RichardSt-Cyr i want find more than 1 newlines in my code and highlight them.)

网友:——查找:替换或找到:替换:找到超过2,改变量词

(原文:find more than one newline in code before - Find: (?:\r?\n[^}\r\n]*){2}\K(?=\}) replace: warning or Find: ((?:\r?\n[^}\r\n]*){2})(?=\}) replace: $1warning To find more than 2, change the quantifier to {2,})

解决方案:
如果你的意思是你想找的地方在您的文件有一个空行一行的第一个非空白字符},下面的程序可能会有所帮助。将与Gnuawk;有更多Posix-compatibleawk你需要使用(或类似的)而不是给定的模式。是一个神奇的工具,但它逐行匹配。它不能识别多行模式,你也不能改变行分隔符。
原文:

If you mean that you would like to find places in your file where there is a blank line before a line whose first non-blank character is a }, the following awk program might help.

awk '/\S/              { blanks = NR - prev - 1; prev = NR }
     blanks && /^\s*}/ { printf "%d: WARNING: Useless blank line\n", NR - 1 }'

That will work with Gnu awk; with more Posix-compatible awks you'll need to use /[^ \t]/ and /^[ \t]*}/ (or something similar) to instead of the given patterns.

egrep is a wonderful tool but it does line-by-line matching. It cannot recognize multiline patterns, nor can you change the line delimiter.

解决方案:
egrep不使用多行。您可以使用sed(或awk先前所回答),而不是“egrep”。不需要使用perl。请参阅下面的脚本。
原文:

egrep does not work with multiple lines. You can use sed (or awk as mentioned in previous answer) instead of "egrep". No need to use perl. See the script below.

TAGS2="}"
echo "searching ${SRCROOT} for ${TAGS2}"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) \
  -print -exec sed "/$TAGS2/{ # search for tag 
    =; # print line number
    N; # join with next line
    s/$TAGS2/warning/; # substitute tags found with warning
  }" {} \;