krouch February 2016

Grep only for the first match in amount of files

In general, I'm looking for a way to show each match of a grep command just once.

For the current usage I intend to have a list of all programmers which contributed files to a database. The files of interest are all written in java, therefor the search pattern is "@author". In the end, I like to get an enumeration of all shortcuts ( at this point I do not even care, in which files the pattern occur). The result should be similar to the example below:

pak@Q:~$ grep -r "@author" | [...]
@bsh
@janS
@Jan Snow
...

So far grep -r "@author" | sort -u didn't do the trick for me.

Answers


Jon Surrell February 2016

Pretty straightforward, you can sort and get unique entries:

grep [...] | sort -u

If you're grepping across multiple files, you'll probably want -h option, and perhaps -s to hide error messages:

Example

For example:

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

From dir we 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 the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search. -s, --no-messages Suppress error messages about nonexistent or unreadable files.

From sort man page:

sort - sort lines of text files

  • -u, --unique

    with -c, check for strict ordering; without -c, output only the first of an equal run

Credit

Thanks to @EdMorton for the sort -u version. Originally I suggested the following (which remains valid):

grep -r "author" | sort | uniq

Post Status

Asked in February 2016
Viewed 1,177 times
Voted 12
Answered 1 times

Search




Leave an answer