Find and Grep Statement - grep

I am trying to find the location and frequency of the following patterns
x37 x41
x42 x43
x44 x45
x46 x63
x75 x76
x81 x82
x83 x95
x96 Bottom1
Bottom2 Bottom3
Middle Middle1
Right1 Top3
in a htdocs directory.
Thanks in advance!

This will give you the locations:
grep -rE '(x(37|4[1-6]|63|7[56]|8[123]|9[56]))|Bottom[123]|Middle1?|Right1|Top3' /directory/to/search
This will give you the frequencies:
grep -orE '(x(37|4[1-6]|63|7[56]|8[123]|9[56]))|Bottom[123]|Middle1?|Right1|Top3' /directory/to/search | sort | uniq -c

Related

Finding consecutive words with grep

I have checked this topic to find two words in a text file: How to combine two search words with "grep" (AND)
But now I'm trying to know if it's possible to check when they are consecutive in the file. For instance:
grep -ilZ "not" file.txt | xargs -0 grep -il "sure"
In file.txt I have "I am not sure".
How could I do?
Thank you so much.
Just grep -il "not sure" file.txt would solve your problem.

Simple Grep Issue

I am trying to parse items out of a file I have. I cant figure out how to do this with grep
here is the syntax
<FQDN>Compname.dom.domain.com</FQDN>
<FQDN>Compname1.dom.domain.com</FQDN>
<FQDN>Compname2.dom.domain.com</FQDN>
I want to spit out just the bits between the > and the <
can anyone assist?
Thanks
grep can do some text extraction. however not sure if this is what you want:
grep -Po "(?<=>)[^<]*"
test
kent$ echo "<FQDN>Compname.dom.domain.com</FQDN>
dquote>
dquote> <FQDN>Compname1.dom.domain.com</FQDN>
dquote>
dquote> <FQDN>Compname2.dom.domain.com</FQDN>"|grep -Po "(?<=>)[^<]*"
Compname.dom.domain.com
Compname1.dom.domain.com
Compname2.dom.domain.com
Grep isn't what you are looking for.
Try sed with a regular expression : http://unixhelp.ed.ac.uk/CGI/man-cgi?sed
You can do it like you want with grep :
grep -oP '<FQDN>\K[^<]+' FILE
Output:
Compname.dom.domain.com
Compname1.dom.domain.com
Compname2.dom.domain.com
As others have said, grep is not the ideal tool for this. However:
$ echo '<FQDN>Compname.dom.domain.com</FQDN>' | egrep -io '[a-z]+\.[^<]+'
Compname.dom.domain.com
Remember that grep's purpose is to MATCH things. The -o option shows you what it matched. In order to make regex conditions that are not part of the expression that is returned, you'd need to use lookahead or lookbehind, which most command-line grep does not support because it's part of PCRE rather than ERE.
$ echo '<FQDN>Compname.dom.domain.com</FQDN>' | grep -Po '(?<=>)[^<]+'
Compname.dom.domain.com
The -P option will work in most Linux environments, but not in *BSD or OSX or Solaris, etc.

Is there a way in grep to find out how many lines matched the grep result?

Suppose I write a grep query to find out the occurrence of a method call on an object like this:
// might not be accurate, but irrelevant
grep -nr "[[:alnum:]]\.[[:alnum:]](.*)" .
This would give many results. How to find out how many such results are obtained?
What about using | wc -l to count the number of result lines?
What about
man grep | grep "count"
It outputs
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. [...]
Previous answers are OK, I just want to put it into command line instructions in order to have copy-paste versions (from explicit to simplest) for the future:
grep --count "PATTERN" FILE
Is exactly the same as:
grep -c "PATTERN" FILE
And it is equivalent to:
grep "PATTERN" FILE | wc -l
As a bonus, below i give you a version where a file with a list of patterns is used.
grep -count --file=PATTERNFILE FILE
or simply
grep -cf PATTERNFILE FILE

Grep for multiple patterns over multiple files

I've been googling around, and I can't find the answer I'm looking for.
Say I have a file, text1.txt, in directory mydir whose contents are:
one
two
and another called text2.txt, also in mydir, whose contents are:
two
three
four
I'm trying to get a list of files (for a given directory) which contain all (not any) patterns I search for. In the example I provided, I'm looking for output somewhere along the lines of:
./text1.txt
or
./text1.txt:one
./text1.txt:two
The only things I've been able to find are concerning matching any patterns in a file, or matching multiple patterns in a single file (which I tried extending to a whole directory, but received grep usage errors).
Any help is much appreciated.
Edit-Things I've tried
grep "pattern1" < ./* | grep "pattern2" ./*
"ambiguous redirect"
grep 'pattern1'|'pattern2' ./*
returns files that match either pattern
One way could be like this:
find . | xargs grep 'pattern1' -sl | xargs grep 'pattern2' -sl
I think this is what you need (you can add easily more patterns)
grep -EH 'pattern1|pattern2' mydir
To refine brain's answer:
find . -type f -print0 | xargs -0 grep 'pattern1' -slZ | xargs -0 grep 'pattern2' -sl
This will keep grep from trying to search directories, and can properly handle filenames with spaces, if you pass the -Z flag to grep for all but the last pattern and pass -0 to xargs.

use grep to return a list of files, given multiple keywords (like google returns a list of webpages)

I need to find ALL files that have multiple keywords anywhere in the file (not necessarily on the same line), given a starting directory like ~/. Does "grep -ro" do this?
(I'm using Unix, Mac OSX 10.4)
You can use the -l option to get a list of filenames with matches, so it's just a matter of finding all of the files that have the first keyword and then filtering that list down to the files that also have the second keyword:
grep -rl first_keyword basedir | xargs grep -l second_keyword
To search just *.txt
find ~/. -name "*.txt" | xargs grep -l first_keyword | xargs grep -l second_keyword
Thanks Adam!

Resources