R7AF
asked on
Grep: show filename and line number
I have 1GB directory which has many files with a string like 'abc123' in it. When I query the files now and pipe the results in a text file, I get a very big file back, which is probably the result of very long lines in the source file.
Now I use: grep -inR 'abc123' *, which returns filename, linenumber, plus the whole line, which can be very long. Is it possible to shorten that line, or even leave it out?
Now I use: grep -inR 'abc123' *, which returns filename, linenumber, plus the whole line, which can be very long. Is it possible to shorten that line, or even leave it out?
can you give example of desired output? you last line is not clear
Use awk to shorten the result.
eg . grep -inR 'abc123' * | awk { print $1 ,$2 }
eg . grep -inR 'abc123' * | awk { print $1 ,$2 }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
But now I have another problem. This prints the count for each file. There are thousands of files with count=0, which I don't want to know about. Can these be left out of the results?
But now I have another problem. This prints the count for each file. There are thousands of files with count=0, which I don't want to know about. Can these be left out of the results?
use awk like this ...
awk ' if ($3 !=0) {print $1, $2 }
awk ' if ($3 !=0) {print $1, $2 }
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... or, with my first suggestion
grep -ciR 'abc123' * |awk -F: '$2!=0 {print}'
This will not display line numbers, however, only the count of matching lines (>0)!
grep -ciR 'abc123' * |awk -F: '$2!=0 {print}'
This will not display line numbers, however, only the count of matching lines (>0)!
ASKER
Thanks for all suggestions. Both these commands do what I need. It turns out that about 20 files have matching results. Because all files have only one match, the first command is more usefull in this case. I will split the points.
grep -inR 'abc123' * |awk -F: '{print $1, $2}'
grep -ciR 'abc123' * |awk -F: '$2!=0 {print}'
grep -inR 'abc123' * |awk -F: '{print $1, $2}'
grep -ciR 'abc123' * |awk -F: '$2!=0 {print}'
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines.
wmp