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.

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

echo $output<br />

fi # sed -ne "/$1/s|^|${file}: |p" equivale al precedente.<br />

echo<br />

done<br />

echo<br />

exit 0<br />

# Esercizi:<br />

# ---------<br />

# 1) Aggiungete nuove righe <strong>di</strong> output nel caso ci sia più <strong>di</strong> una<br />

#+ occorrenza per il file dato.<br />

# 2) Aggiungete altre funzionalità.<br />

È possibile far ricercare a grep due (o più) <strong>di</strong>fferenti modelli? Cosa si può fare se volessimo che<br />

grep visualizzi tutte le righe <strong>di</strong> un file o i file che contengono sia “modello1” che “modello2”?<br />

Un metodo consiste nel collegare con una pipe il risultato <strong>di</strong> grep modello1 a grep modello2.<br />

Ad esempio, dato il file seguente:<br />

# File: tstfile<br />

Questo è un file d’esempio.<br />

Questo è un file <strong>di</strong> testo or<strong>di</strong>nario.<br />

Questo file non contiene testo strano.<br />

Questo file non è insolito.<br />

Altro testo.<br />

Ora cerchiamo nel file le righe contenenti entrambe le parole “file” e “testo” . . .<br />

bash$ grep file tstfile<br />

# File: tstfile<br />

Questo è un file d’esempio.<br />

Questo è un file <strong>di</strong> testo or<strong>di</strong>nario.<br />

Questo file non contiene testo strano.<br />

Questo file non è insolito..<br />

bash$ grep file tstfile | grep testo<br />

Questo è un file <strong>di</strong> testo or<strong>di</strong>nario.<br />

Questo file non contiene testo strano.<br />

--<br />

258

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

Saved successfully!

Ooh no, something went wrong!