11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

grep<br />

Ve<strong>di</strong> anche Esempio 12-5, Esempio 12-35 e Esempio 29-6.<br />

Capitolo 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

Strumento <strong>di</strong> ricerca multifunzione che fa uso delle Espressioni Regolari. In origine era un<br />

comando/filtro del venerabile e<strong>di</strong>tor <strong>di</strong> linea ed: g/re/p -- global - regular expression - print.<br />

grep modello [file...]<br />

Ricerca nel/nei file in<strong>di</strong>cato/i l’occorrenza <strong>di</strong> modello, dove modello può essere o un testo<br />

letterale o un’Espressione Regolare.<br />

bash$ grep ’[rst]ystem.$’ osinfo.txt<br />

The GPL governs the <strong>di</strong>stribution of the Linux operating system.<br />

Se non vengono specificati i file, grep funziona come filtro sullo stdout, come in una pipe.<br />

bash$ ps ax | grep clock<br />

765 tty1 S 0:00 xclock<br />

901 pts/1 S 0:00 grep clock<br />

L’opzione -i abilita una ricerca che non fa <strong>di</strong>stinzione tra maiuscole e minuscole.<br />

L’opzione -w verifica solo le parole esatte.<br />

L’opzione -l elenca solo i file in cui la ricerca ha avuto successo, ma non le righe verificate.<br />

L’opzione -r (ricorsivo) ricerca i file nella <strong>di</strong>rectory <strong>di</strong> lavoro corrente e in tutte le sue<br />

sotto<strong>di</strong>rectory.<br />

L’opzione -n visualizza le righe verificate insieme al loro numero.<br />

bash$ grep -n Linux osinfo.txt<br />

2:This is a file containing information about Linux.<br />

255

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!