command line - Grep only for the first match in amount of files -


in general, i'm looking way show each match of grep command once.

for current usage intend have list of programmers contributed files database. files of interest written in java, therefor search pattern "@author". in end, enumeration of shortcuts ( @ point not care, in files pattern occur). result should similar example below:

pak@q:~$ grep -r "@author" | [...] @bsh @jans @jan snow ... 

edit: in case facing similar problem, command of interest

grep -rh "@author" | sort -u

pretty straightforward, can sort , unique entries:

grep [...] | sort -u 

if you're grepping across multiple files, you'll want -h option, , perhaps -s hide error messages:

example

for example:

dir ├── │   └── file contents: │       @author ed │       @author frank │       @author ben │      └── b     └── file contents:         @author ben         @author frank         @author steve 

from dir run

$ grep -sh '@author' * | sort -u 

output:

@author ben @author ed @author frank @author steve 

more info

from grep man page:

-h, --no-filename suppress prefixing of file names on output. default when there 1 file (or standard input) search. -s, --no-messages suppress error messages nonexistent or unreadable files.

from sort man page:

sort - sort lines of text files

  • -u, --unique

    with -c, check strict ordering; without -c, output first of equal run

credit

thanks @edmorton sort -u version. suggested following (which remains valid):

grep -r "author" | sort | uniq 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -