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?
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?
With grep
s 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).
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?
– mikeserv
Dec 11 '15 at 18:18
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
– Stéphane Chazelas
Dec 11 '15 at 18:27
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
awk
would be more like awk 'length > 100 && /if/' file
. Note that for /\<if\>/
, you need GNU awk
.
– Stéphane Chazelas
Dec 10 '15 at 17:39
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).
if
in their path
– Stéphane Chazelas
Dec 10 '15 at 13:52
':.*if'
to prevent that (unless your filenames contain colons).
– choroba
Dec 10 '15 at 13:53
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
sed -n '/.\{100\}/{/if/p}' file
. Similarly the perl one: perl -nle 'print if length$_>99 && /if/'
– Toby Speight
Dec 10 '15 at 17:59
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 grep
s, though.
find . -type f -exec awk 'length > 100 && /if/' {} +
– Stéphane Chazelas Dec 10 '15 at 13:50