-1

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/"
Zept
  • 1,158
  • 1
  • 10
  • 14
  • 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. – rici Dec 17 '15 at 21:36
  • 2
    Can you include an input and expected output? – Richard St-Cyr Dec 17 '15 at 21:42
  • @RichardSt-Cyr i want find more than 1 newlines in my code and highlight them. – Zept Dec 17 '15 at 21:46
  • `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,}` –  Dec 17 '15 at 21:53

2 Answers2

0

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.

rici
  • 234,347
  • 28
  • 237
  • 341
0

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
  }" {} \;