2010-02-21 02:41:41 +0000 2010-02-21 02:41:41 +0000
79
79
Advertisement

Come posso filtrare i risultati unici dall'output di grep?

Advertisement

In linux, posso fare il grep di una stringa da un file usando grep mySearchString myFile.txt. Come posso ottenere solo i risultati che sono unici?

Advertisement
Advertisement

Risposte (2)

132
132
132
2010-02-21 02:52:47 +0000

Potete ottenere questo con le utility sort e uniq .

esempio:

[john@awesome ~]$ echo -e "test\ntest\ntest\nanother test\ntest" test test test another test test [john@awesome ~]$ echo -e "test\ntest\ntest\nanother test\ntest" | sort | uniq another test test

a seconda dei dati potresti voler utilizzare anche alcuni degli interruttori.

3
3
3
2019-08-22 21:13:48 +0000

Potete usare:

grep -rohP "(mySearchString)" . | sort -u

-r: ricorsivo

-o: stampa solo la parte di testo corrispondente

-h: non stampa i nomi dei file

-P: regex in stile Perl (puoi usare invece -E a seconda del tuo caso)

sort -u è meglio di sort | uniq, come sottolineato da @Chris Johnsen.

Advertisement

Domande correlate

6
10
7
5
12
Advertisement