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.

tr<br />

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

Uso <strong>di</strong> wc per contare quanti file .txt sono presenti nella <strong>di</strong>rectory <strong>di</strong> lavoro corrente:<br />

$ ls *.txt | wc -l<br />

# Il conteggio si interrompe se viene trovato un carattere <strong>di</strong><br />

#+ linefeed nel nome <strong>di</strong> uno dei file "*.txt".<br />

# Mo<strong>di</strong> alternativi per svolgere lo stesso compito:<br />

# find . -maxdepth 1 -name \*.txt -print0 | grep -cz .<br />

# (shopt -s nullglob; set -- *.txt; echo $#)<br />

# Grazie, S.C.<br />

Uso <strong>di</strong> wc per calcolare la <strong>di</strong>mensione totale <strong>di</strong> tutti i file i cui nomi iniziano con le lettere comprese<br />

nell’intervallo d - h.<br />

bash$ wc [d-h]* | grep total | awk ’{print $3}’<br />

71832<br />

Uso <strong>di</strong> wc per contare le occorrenze della parola “Linux” nel file sorgente <strong>di</strong> questo libro.<br />

bash$ grep Linux abs-book.sgml | wc -l<br />

50<br />

Ve<strong>di</strong> anche Esempio 12-35 e Esempio 16-8.<br />

Alcuni coman<strong>di</strong> possiedono, sotto forma <strong>di</strong> opzioni, alcune delle funzionalità <strong>di</strong> wc.<br />

... | grep foo | wc -l<br />

# Questo costrutto, frequentemente usato, può essere reso in modo più conciso.<br />

... | grep -c foo<br />

# Un semplice impiego dell’opzione "-c" (o "--count") <strong>di</strong> grep.<br />

# Grazie, S.C.<br />

filtro per la sostituzione <strong>di</strong> caratteri.<br />

263

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

Saved successfully!

Ooh no, something went wrong!