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 in find, , -0 used xargs. both of these non-posix extensions, it's better use posix-compliant -exec ... {} + action in find xargs behavior of running few subprocesses possible names given literally.
  • the grep ... -- /dev/null idiom services 2 purposes: passing /dev/null ensures grep's defaults uses when passed @ least 2 files, if find invoked particular grep instance single filename. -- flag, contrast, ensures subsequent filenames treated names, not flags; it's not strictly necessary when used find in manner (as arguments preceded ./, not potentially looking optional arguments), practice nonetheless.

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -