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 />

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

L’opzione -v (o --invert-match) scarta le righe verificate.<br />

grep modello1 *.txt | grep -v modello2<br />

# Verifica tutte le righe dei file "*.txt" contenenti "modello1",<br />

# ma ***non*** quelle contenenti "modello2".<br />

L’opzione -c (--count) fornisce il numero delle occorrenze, ma non le visualizza.<br />

grep -c txt *.sgml # ((numero <strong>di</strong> occorrenze <strong>di</strong> "txt" nei file "*.sgml")<br />

# grep -cz .<br />

# ^ punto<br />

# significa conteggio (-c) zero-<strong>di</strong>viso (-z) elementi da cercare "."<br />

# cioè, quelli non vuoti (contenenti almeno 1 carattere).<br />

#<br />

printf ’a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf’ | grep -cz . # 3<br />

printf ’a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf’ | grep -cz ’$’ # 5<br />

printf ’a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf’ | grep -cz ’^’ # 5<br />

#<br />

printf ’a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf’ | grep -c ’$’ # 9<br />

# Per default, i caratteri <strong>di</strong> a capo (\n) separano gli elementi da cercare.<br />

# Notate che l’opzione -z è specifica del "grep" <strong>di</strong> GNU.<br />

# Grazie, S.C.<br />

Quando viene invocato con più <strong>di</strong> un file, grep specifica qual’è il file contenente le occorrenze.<br />

bash$ grep Linux osinfo.txt misc.txt<br />

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

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

misc.txt:The Linux operating system is stea<strong>di</strong>ly gaining in popularity.<br />

Suggerimento: Per forzare grep a visualizzare il nome del file quando ne è presente soltanto<br />

uno, si deve in<strong>di</strong>care come secondo file /dev/null<br />

bash$ grep Linux osinfo.txt /dev/null<br />

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

256

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

Saved successfully!

Ooh no, something went wrong!