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:
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:
│ └── File contents:
│ @author ed
│ @author frank
│ @author ben
└── File contents:
From dir we run
$ grep -sh '@author' * | sort -u
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
with -c, check for strict ordering; without -c, output only the first of an equal run
Thanks to @EdMorton for the sort -u version. Originally I suggested the following (which remains valid):
grep -r "author" | sort | uniq
Asked in February 2016Viewed 1,177 timesVoted 12Answered 1 times