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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C.2. Awk<br />

12. Esempio A-1<br />

13. Esempio 12-13<br />

14. Esempio 12-11<br />

15. Esempio A-10<br />

16. Esempio 17-12<br />

17. Esempio 12-16<br />

18. Esempio A-28<br />

Appen<strong>di</strong>ce C. Una breve introduzione a Sed e Awk<br />

Per una spiegazione più ampia <strong>di</strong> sed, si controllino i relativi riferimenti in Bibliografia.<br />

Awk è un linguaggio completo per l’elaborazione <strong>di</strong> testo, con una sintassi che ricorda quella del C.<br />

Sebbene possegga un’ampia serie <strong>di</strong> funzionalità e <strong>di</strong> operatori, qui ne verranno analizzati solo un paio -<br />

quelli più utili allo <strong>scripting</strong> <strong>di</strong> shell.<br />

Awk sud<strong>di</strong>vide ogni riga dell’input che gli è stato passato in campi. Normalmente, un campo è una<br />

stringa <strong>di</strong> caratteri consecutivi separati da spazi, anche se esistono opzioni per mo<strong>di</strong>ficare il delimitatore.<br />

Awk, quin<strong>di</strong>, analizza e agisce su ciascun singolo campo. Questo lo rende ideale per trattare file <strong>di</strong> testo<br />

strutturati -- in particolare le tabelle -- e dati organizzati in spezzoni logici, come righe e colonne.<br />

Negli script <strong>di</strong> shell, i segmenti <strong>di</strong> co<strong>di</strong>ce awk vengono racchiusi da apici singoli (quoting forte) e da<br />

parentesi graffe.<br />

echo uno due | awk ’{print $1}’<br />

# uno<br />

echo uno due | awk ’{print $2}’<br />

# due<br />

awk ’{print $3}’ $nomefile<br />

# Visualizza allo stdout il campo nr.3 del file $nomefile.<br />

awk ’{print $1 $5 $6}’ $nomefile<br />

# Visualizza i campi nr.1, 5 e 6 del file $nomefile.<br />

771

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

Saved successfully!

Ooh no, something went wrong!