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.

•<br />

•<br />

•<br />

echo a111b | sed -ne ’/a1\+b/p’<br />

echo a111b | grep ’a1\+b’<br />

echo a111b | gawk ’/a1+b/’<br />

# Tutte queste forme si equivalgono.<br />

# Grazie, S.C.<br />

Capitolo 19. Espressioni Regolari<br />

“Parentesi graffe” con escaping -- \{\} -- in<strong>di</strong>cano il numero <strong>di</strong> occorrenze da verificare nella ER che<br />

le precede.<br />

L’escaping delle parentesi graffe è necessario perché, altrimenti, avrebbero semplicemente il loro<br />

significato letterale. Quest’uso, tecnicamente, non fa parte della serie <strong>di</strong> ER <strong>di</strong> base.<br />

“[0-9]\{5\}” verifica esattamente cinque cifre (nell’intervallo da 0 a 9).<br />

Nota: Le parentesi graffe non sono <strong>di</strong>sponibili come ER nella versione “classica” (non-POSIX<br />

compliant) <strong>di</strong> awk. Comunque, gawk possiede l’opzione --re-interval che le consente (senza<br />

dover usare l’escaping).<br />

bash$ echo 2222 | gawk --re-interval ’/2{3}/’<br />

2222<br />

Perl ed alcune versioni <strong>di</strong> egrep non richiedono l’escaping delle parentesi graffe.<br />

Parentesi -- ( ) -- racchiudono gruppi <strong>di</strong> ER. Sono utili seguite dall’operatore “|” e nelle estrazioni <strong>di</strong><br />

sottostringa che usano expr.<br />

-- | -- l’operatore “or” delle ER verifica una serie qualsiasi <strong>di</strong> caratteri alternativi.<br />

bash$ egrep ’(l|r)egge’ misc.txt<br />

La persona che legge sembra essere meglio informata <strong>di</strong> chi non lo fa.<br />

Il re saggio regge il proprio regno con giustizia.<br />

428

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

Saved successfully!

Ooh no, something went wrong!