How to find lines containing more than 100 characters and contains "if"?

6,830

Solution 1

You can use two greps connected by a pipe:

grep -r '.\{100\}' /path | grep 'if'

To exclude files with if in their paths or names, use ':.*if' instead of 'if' (could still break if your filenames or paths contain colons).

Solution 2

With greps that support the -r (recursive) and -P (PCRE) options (or pcregrep with -r):

grep -rP '^(?=.{101}).*?if' .

Or POSIXly:

find . -type f -exec awk 'length > 100 && /if/ {
   print FILENAME ": " $0}' {} +

(note that the behaviour will vary between implementations for non-text files (files containing non-characters, zero byte values, too long lines or data after the last newline). Also note that some grep implementations will search in non-regular files or will follow symbolic links).

Solution 3

Use awk to count size of $0 and presence of substring if?
awk '( length($0) > 100 && index($0,"if") ){print}' file

If "if" should be a word (as opposed to a simple substring), you could use awk '( length($0) > 100 && match($0,/\<if\>/) ){print}' file

Solution 4

with a single grep:

grep -vxE '.{0,99}|([^i]|i[^f])*i*' <in >out

that will only select lines which cannot be described from head to tail with either statement. and so any line which can be described as consisting of between 0 and 99 characters will not be selected, and similarly any line which matches more than 99 characters and yet still does not contain at least a single if will also fail to be selected.

printf '^%-100b$\n' 'if\nif' 'hey if' i if |
grep -nvxE '.{0,99}|([^i]|i[^f])*i*'

3:^hey if                                                                                              $
5:^if                                                                                                  $

you might do better just to use two greps, though.

Solution 5

Adapted from Find any lines exceeding a certain length any of the following will work to find lines longer than 100 chars

grep '.\{100\}' file

perl -nle 'print if length$_>99' file

awk 'length($0)>99' file

sed -n '/.\{100\}/p' file

chose your preferred method and pipe it through grep if

Share:
6,830

Related videos on Youtube

pushandpop
Author by

pushandpop

T-1000 is my worsest nightmare.

Updated on September 18, 2022

Comments

  • pushandpop
    pushandpop over 1 year

    So, probably I should use grep for this. As long as I need recursive search I should use grep -r. But then I don't know what should I do next ;)

    How can I do that?

    • Admin
      Admin over 8 years
      I wince at the thought of ever having to search through a codebase for Lines with an if statement more then 100 characters to refactor :-Z
    • Admin
      Admin over 8 years
      @Falco The statement itself could be simple, just indented out quite a bit.
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    Note that it could report all the 100 character lines in files that have if in their path
  • choroba
    choroba over 8 years
    @StéphaneChazelas: True. You can use ':.*if' to prevent that (unless your filenames contain colons).
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    The idiomatic way to write it in awk would be more like awk 'length > 100 && /if/' file. Note that for /\<if\>/, you need GNU awk.
  • Dani_l
    Dani_l over 8 years
    @StéphaneChazelas yeah, I saw your syntax and learned the right way, but since you already posted an answer I didn't see the point in modifying mine. I did upvote your answer, though.
  • Toby Speight
    Toby Speight over 8 years
    The sed version can do both checks at once; example using GNU extensions: sed -n '/.\{100\}/{/if/p}' file. Similarly the perl one: perl -nle 'print if length$_>99 && /if/'
  • mikeserv
    mikeserv over 8 years
    I'm only aware of one grep that does, and even that one requires its support built-in to the lib c to work as I understand it. Are there more?
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    @mikeserv, not sure what you mean with lib c. PCRE regexp support is generally provided by libpcre. An exception is the grep from ast-open that has its own implementation of perl-like regexps. grep implementations that support grep -rP do include GNU, FreeBSD/OS/X (a rewrite of GNU grep now diverging) and ast-open's