llrs
llrs

Reputation: 3397

Search just one into each file

I am trying to know which files I need to edit, they follow a pattern that it is easy to search with grep (grep -rnw . -e "text"), but it returns several times the same path to the file for each match.

How can I avoid it?

Example:

./rnaspace_cli.py:41:from rnaspace.core.id_tools import id_tools
./rnaspace_cli.py:42:from rnaspace.core.sequence import sequence
./rnaspace_cli.py:44:from rnaspace.core.trace.event import add_seq_event
./rnaspace_cli.py:45:from rnaspace.core.trace.event import disk_error_event
./rnaspace_on_web:33:from rnaspace.ui.web.controller.rnaspace_controller import rnaspace_controller

Desired output:

./rnaspace_cli.py:41:from rnaspace.core.id_tools import id_tools
./rnaspace_on_web:33:from rnaspace.ui.web.controller.rnaspace_controller import rnaspace_controller

Or even better just the path and files:

./rnaspace_cli.py
./rnaspace_on_web

Upvotes: 0

Views: 44

Answers (3)

chaos
chaos

Reputation: 9262

This should work:

grep -rnw . -e "text" | awk -F: '{print $1}' | uniq

Edit Explanation:

  • awk -F: '{print $1}' - Splits the output at the :-sign and prints only the first part
  • uniq - show repeated lines only one time

Upvotes: 1

anubhava
anubhava

Reputation: 784888

Use -l option in grep to get the file names only in outout:

grep -lrnw . -e "text"

Upvotes: 0

clt60
clt60

Reputation: 63892

From the grep man page:

 -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.)

so,

grep -l 'pattern' files*

will show only filenames what contains a pattern

Upvotes: 3

Related Questions