grep listing filenames without content - search

How can we list only filenames after searching using grep?
When I use
grep -i -R "search keyword" folder
it'll list all the inline lines of code also.

From grep man page:
-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The
scanning will stop on the first match.
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning
will stop on the first match. (-l is specified by POSIX.)
For you, -l option would be helpful.

Use
grep -i -R -l "search keyword" location
to get the list of files which contain the keyword.

Related

List each file that doesn't match a pattern recursively

Tried the following command, it lists all the lines including file names
which are not matching the given pattern.
grep -nrv "^type.* = .*"
"But what we need is list of file names in a folder with content
which does not have even a single occurrence of above pattern."
Your help will be really appreciated.
You need the -L option:
grep -rL '^type.* = .*' directory_name
From the GNU grep manual:
-L, - -files-without-match
    Suppress normal output; instead print the name of each input file from which no output    would normally have been printed. The scanning will stop on the first match.

egrep not giving result full path in for loop

I have a simple script return full path if it found in egrep. However it returns only count for each file.
#!/bin/bash
for file in `/logs/*`
do
egrep "Warning" $file | wc -l ;
done
return full path if it found in egrep
To return the filenames of files that contain the string Warning from files inside logs/* just do:
grep -l "Warning" /logs/*
The -l option to makes grep output only the filenames that match the regex, see man grep. The /logs/* expands to the list of all filenames inside the /logs/ directory, see bash filename expansion.
Please don't use egrep. According to man egrep is deprecated and grep -E is preferred. The Warning is a plain text without any significant regular expressions - there is no need to parse it as an extended regular expression.
Please don't use ` backticks, they are discouraged. The $(...) process substitution is preferred.

Why doesn't grep -lv work?

I want to print out the name of a file if and only if it does not contain the string foo. However, if file contains foo and I run this
grep -lv 'foo' file
file is outputted. Why does this happen and what can I do to work around it?
-v means to match any line that doesn't match the pattern. So -lv means to list any file that contains any line that doesn't match the pattern. That's not the same as a file where none of the lines match the pattern.
Use the -L option to list all files that don't have any match for the pattern.
grep -L 'foo' file
-L, --files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The
scanning will stop on the first match.
Don't use -v with this. -L already inverts which files are listed, and -v inverts the way lines are matched, so -Lv is the same as -l.
grep -lv means: list every file that contains a line that does not match.
You're looking for grep -L.
grep -L 'foo' file
you should remove -v option, because it means it will omit the text which is given in any line. So you should try:
grep -L 'foo' Input_file
Also if you want to know all the files which are having string(foo) in a directory or etc then you could try following too.
grep -L 'foo' /path/to/files/*.txt
An example of grep -L where it will print all the .txt file names which have string foo in them.
EDIT: If in case you are interested in awk solution, you could try following too.
awk 'val{close(val)} FNR==1{val=FILENAME} /Var/{;print FILENAME;nextfile}' *.txt

Taking result of a grep search and using it to search another file. linux bash script

I am trying to search the file dep/playlist for 'ohn'. Then, I would like to take this result and apply it to a new grep command that searches the file employee list end then echoes the results on screen. The code below isn't behaving as expected.
grep ohn dep/playlist > search
grep $(cat search) employeelist > newlist
cat newlist
Thanks,
Tim
You need to tell grep to obtain the patterns from a file, use the -f option:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
So the command would look like:
grep -f search employeelist > newlist
Using process substitution you could obviate the need of a temporary file. So the two grep commands could be written into one as:
grep -f <(grep ohn dep/playlist) employeelist > newlist
xargs:
grep ohn dep/playlist | xargs -I name grep name employeelist
This searches dep/playlist for 'ohn', and then upon finding a result, that result is then used in grep X employeelist, where X is the result from the first grep.

Output grep results to text file, need cleaner output

When using the Grep command to find a search string in a set of files, how do I dump the results to a text file?
Also is there a switch for the Grep command that provides cleaner results for better readability, such as a line feed between each entry or a way to justify file names and search results?
For instance, a away to change...
./file/path: first result
./another/file/path: second result
./a/third/file/path/here: third result
to
./file/path: first result
./another/file/path: second result
./a/third/file/path/here: third result
grep -n "YOUR SEARCH STRING" * > output-file
The -n will print the line number and the > will redirect grep-results to the output-file.
If you want to "clean" the results you can filter them using pipe | for example:
grep -n "test" * | grep -v "mytest" > output-file
will match all the lines that have the string "test" except the lines that match the string "mytest" (that's the switch -v) - and will redirect the result to an output file.
A few good grep-tips can be found in this post
Redirection of program output is performed by the shell.
grep ... > output.txt
grep has no mechanism for adding blank lines between each match, but does provide options such as context around the matched line and colorization of the match itself. See the grep(1) man page for details, specifically the -C and --color options.
To add a blank line between lines of text in grep output to make it easier to read, pipe (|) it through sed:
grep text-to-search-for file-to-grep | sed G

Resources