Find inside files two different patterns in the same line and for matched files show number of matched lines

find . -name '*' -type f -print0 | xargs -0 grep -n pattern1 | grep pattern2
The option -print0 for find and -0 for grep help prevent issue with weird characters or spaces in filenames. Furthermore with xargs there is no limited number of arguments that find can throw.

1
By: bugmenot
2012-07-29 00:40:06

These Might Interest You

  • It is often recommended to enclose capital letters in a BibTeX file in braces, so the letters will not be transformed to lower case, when imported from LaTeX. This is an attempt to apply this rule to a BibTeX database file. DO NOT USE sed '...' input.bib > input.bib as it will empty the file! How it works: /^\s*[^@%]/ Apply the search-and-replace rule to lines that start (^) with zero or more white spaces (\s*), followed by any character ([...]) that is *NOT* a "@" or a "%" (^@%). s=<some stuff>=<other stuff>=g Search (s) for some stuff and replace by other stuff. Do that globally (g) for all matches in each processed line. \([A-Z][A-Z]*\)\([^}A-Z]\|},$\) Matches at least one uppercase letter ([A-Z][A-Z]*) followed by a character that is EITHER not "}" and not a capital letter ([^}A-Z]) OR (|) it actually IS a "}", which is followed by "," at the end of the line ($). Putting regular expressions in escaped parentheses (\( and \), respectively) allows to dereference the matched string later. {\1}\2 Replace the matched string by "{", followed by part 1 of the matched string (\1), followed by "}", followed by the second part of the matched string (\2). I tried this with GNU sed, only, version 4.2.1. Show Sample Output


    1
    sed '/^\s*[^@%]/s=\([A-Z][A-Z]*\)\([^}A-Z]\|},$\)={\1}\2=g' literature.bib > output.bib
    michelsberg · 2013-01-15 22:24:17 2
  • The regular expression matches patterns of .c, .cpp, .C, .Cpp, .h, .hpp, .H, .Hpp. The matched files are piped through etags to create a TAGS file, useful for emacs. Alternate regex (if you aren't worried about capital .Cpp) is -regex ".*\.c\|.*\.cpp\|.*\.h"


    0
    find . -regex ".*\.[cChH]\(pp\)?" -print | etags -
    chidarste · 2009-02-05 16:27:26 0

  • -6
    grep -A 3 -i "example" demo_text
    techie · 2013-05-09 08:20:31 0
  • Search in all html files and remove the lines that 'String' is found.


    -3
    for i in $(find . -iname '*.html'); do sed '/String/d' $i > $i-tmp; mv $i-tmp $i; done
    cadu · 2010-09-21 14:35:18 4

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: