24

I want to look up a word in multiple files, and return only a single line per result, or a limited number of characters (40 ~ 80 characters for example), and not the entire line, as by default.

grep -sR 'wp-content' .

file_1.sql:3309:blog/wp-content 
file_1.sql:3509:blog/wp-content 
file_2.sql:309:blog/wp-content 

Currently I see the following:

grep -sR 'wp-content' .

file_1.sql:3309:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without.
file_1.sql:3509:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without. 
file_2.sql:309:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without.
Patrick Maciel
  • 4,874
  • 8
  • 40
  • 80

3 Answers3

22

You could use a combination of grep and cut

Using your example I would use:

grep -sRn 'wp-content' .|cut -c -40
grep -sRn 'wp-content' .|cut -c -80

That would give you the first 40 or 80 characters respectively.

edit:

Also, theres a flag in grep, that you could use:

-m NUM, --max-count=NUM
          Stop reading a file after NUM matching lines.

This with a combination of what I previously wrote:

grep -sRnm 1 'wp-content' .|cut -c -40
grep -sRnm 1 'wp-content' .|cut -c -80

That should give you the first time it appears per file, and only the first 40 or 80 chars.

aemus
  • 713
  • 4
  • 14
20
 egrep -Rso '.{0,40}wp-content.{0,40}' *.sh

This will not call the Radio-Symphonie-Orchestra, but -o(nly matching).

A maximum of 40 characters before and behind your pattern. Note: *e*grep.

user unknown
  • 35,537
  • 11
  • 75
  • 121
4

If you change the regex to '^.*wp-content' you can use egrep -o. For example,

egrep -sRo '^.*wp-content' .

The -o flag make egrep only print out the portion of the line that matches. So matching from the start of line to wp-content should yield the sample output in your first code block.

Tim Pote
  • 27,191
  • 6
  • 63
  • 65
  • I may not have correctly understood the command, but did not work, he still listing the same way. – Patrick Maciel May 08 '12 at 12:47
  • If your content is what you have in your example, it should output everything up to `wp-content`, and nothing past that. That should be the same output in your first code block. However, judging by the other answers, that may or may not be what you actually want. – Tim Pote May 08 '12 at 13:19
  • @PatrickMaciel See my changes. I expanded my explanation of the `-o` flag. – Tim Pote May 08 '12 at 14:02
  • This doesn't limit the line length, unfortunately, so if what you're searching for shows up somewhere in a minified file it'll still show a wall of text. You could modify it to something like `'.{0,50}wp-content.{0,50}'`, but performance on that is pretty bad. – lobati Apr 13 '17 at 19:06