Link to home
Start Free TrialLog in
Avatar of R7AF
R7AFFlag for Netherlands

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?
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

-c, --count
      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

can you give example of desired output? you last line is not clear
Avatar of magento
magento

Use awk to shorten the result.

eg . grep -inR 'abc123' * | awk { print $1 ,$2 }
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of R7AF

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?
use awk like this ...

awk '  if ($3 !=0) {print $1, $2 }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... 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)!
 
Avatar of R7AF

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}'