linux - Matching file extensions inside content with grep ('*.log' is an error) -
i'm trying search each line see lines have any_filename.log
the below codes aren't working
find . -print | xargs grep -i *.log find . -print | xargs grep -i "*.log" find . -print | xargs grep -i '*.log'
does know right code?
thanks you
you don't need *
@ all; doesn't useful here. in regex syntax (as used grep), *
means "0 or more of preceding character". when there is no preceding character, syntax error.
by contrast, .
in regex means "match single character". if want match period, need escape or put inside character class, so:
find . -exec grep -i -e '[.]log' -- /dev/null '{}' +
if line needs end with .log
, contrast, anchor match $
:
find . -exec grep -i -e '[.]log$' -- /dev/null '{}' +
notes:
*.log
glob syntax.[.]log$
equivalent pattern in regex (regular expression) syntax. these 2 different languages.find ... | xargs ...
buggy (fails filenames containing spaces, quotes, literal backslashes, etc) unless-print0
used infind
, ,-0
usedxargs
. both of these non-posix extensions, it's better use posix-compliant-exec ... {} +
action infind
xargs behavior of running few subprocesses possible names given literally.- the
grep ... -- /dev/null
idiom services 2 purposes: passing/dev/null
ensuresgrep
's defaults uses when passed @ least 2 files, iffind
invoked particulargrep
instance single filename.--
flag, contrast, ensures subsequent filenames treated names, not flags; it's not strictly necessary when usedfind
in manner (as arguments preceded./
, not potentially looking optional arguments), practice nonetheless.
Comments
Post a Comment