Grep command- How to use in different directories - linux

How to grep single word in different directory and file name is also same in different directory.

I'm not sure to understand what you want.
Can't you just run
grep -rwn word /some/dir1/ /other/dir2/
Read the GNU grep documentation!

If you are using bash you can use the following:
grep 'pattern' {dir1,dir2,dir3}/filename

Specify each file as an argument:
grep -w vwhat /some/file /another/file /a/third/file

Related

how to display file name during grep

Here is my current code and I also prefer to show matched file name as well (if the content of the file matched grep), any solutions are appreciated.
for file in *.py;
do grep -n --color 'main' $file;
done
BTW, I am using Linux/OS X.
thanks in advance,
Lin
If you're using GNU grep, you can use the -H or --with-filename option to force it to display the filename even when there's only one file argument.
for file in *.py; do
grep -H -n --color 'main' $file
done
Both Linux and OS X use GNU grep, so it should work in both environments.
I'm sure this has been on here before, but you just need to give it a second file name
for file in *.py;
do grep -n --color 'main' $file /dev/null;
done

linux command for finding files containing a specific word

basically what is says in the title, but i mean look for files CONTAINING a word, not in the filename but in the content. Is that even possible?
The easiest way is with grep ;)
grep -r word DIR
-r is recursive
But there's also ack that is working pretty closely as grep
You can use rgrep.
rgrep pattern path
which is equivalent to
grep -r pattern path
If you only want to grep for whole words matching, you can add -w to the grep options.
I use grep for that
grep -Ri "keyword" [dir]
-R recursive
-i ignore case

how can I extract a pattern only using grep

How can we extract the contents present inside KeyProviderType tag only using grep command from the folllowing pattern?
<ContentProtectKeyProfiles-row><Name>PREM7</Name><Domain>42.0.112.121</Domain<ProfileType>4</ProfileType>
<Protocol>HTTP</Protocol><Port>80</Port><KeyProviderType>HLS-AES-128</KeyProviderType</ContentProtectKeyProfiles-row>
a#x:/tmp$ cat s.xml
<ContentProtectKeyProfiles-row> <Name>PREM7</Name> <Domain>42.0.112.121</Domain> <ProfileType>4</ProfileType> <Protocol>HTTP</Protocol> <Port>80</Port> <KeyProviderType>HLS-AES-128</KeyProviderType> </ContentProtectKeyProfiles-row>dhruv#dhruv-pathak:/tmp$
a#x:/tmp$ cat s.xml | grep -oe "<KeyProviderType>.*</KeyProviderType>"
<KeyProviderType>HLS-AES-128</KeyProviderType>
Don't use grep to process XML files. Use a proper XML parser. For example, using xsh, I can just run
open in.xml ;
echo (//KeyProviderType) ;
BTW, I had to fix 2 tags that were missing > in your input.
You can try to use gnu awk (due to RS)
awk -v RS="KeyProviderType" 'NR%2==0 {gsub(/>|<\//,"");print}' file
HLS-AES-128
You may use regex lookahead's and lookbehind's, provided your grep support -P flag.
cat s.xml | grep -oP "(?<=<KeyProviderType>).*(?=</KeyProviderType>)"

Is it possible to use "AND " and "NOT" condition in the same grep command

I need to search in a directory of files which has pattern1 but not pattern2.
look at the -v flag to grep. You can pipe multiple calls to grep together, which is probably the simplest approach here. One to look for pattern1, and another to grep -v pattern2.
grep pattern1 $(grep -L pattern2 *)
is probably the easiest way to do it, if I understand correctly what you want. -L means "print just the names of all files that do not contain this pattern"; it's the inverse of -l. This will not work correctly if you have files with whitespace or some other shell metacharacters in their names.
You can add a grep to the first grep:
grep -r "this pattern" /path | grep -v "not this patten"
HTH
Francisco

Use command grep and locate

How I can make the grep command locate certain words in the files specified by the routes found by the locate command?
locate my.cnf | grep user
(I want that grep command search the word "user" on the files found for locate command)
Try:
locate my.cnf | xargs grep user
Instead of a pipe, use command replacement:
grep user `locate my.cnf`
In order to play nice with situations when locate results have spaces in names, you could do this
locate -0 my.cnf | xargs -n1 -0 grep user
Probably grep user $(locate my.cnf) is what you're looking for, if I understand your question correctly.

Resources