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.

Sostituzione <strong>di</strong> sottostringa<br />

${stringa/sottostringa/sostituto}<br />

${stringa//sottostringa/sostituto}<br />

Sostituisce la prima occorrenza <strong>di</strong> $sottostringa con $sostituto.<br />

Sostituisce tutte le occorrenze <strong>di</strong> $sottostringa con $sostituto.<br />

stringaZ=abcABC123ABCabc<br />

${stringa/#sottostringa/sostituto}<br />

Capitolo 9. Variabili riviste<br />

echo ${stringaZ/abc/xyz} # xyzABC123ABCabc<br />

# Sostituisce la prima occorrenza <strong>di</strong> ’abc’ con ’xyz’.<br />

echo ${stringaZ//abc/xyz} # xyzABC123ABCxyz<br />

# Sostituisce tutte le occorrenze <strong>di</strong> ’abc’ con ’xyz’.<br />

Se $sottostringa viene verificata all’inizio <strong>di</strong> $stringa, allora $sostituto rimpiazza<br />

$sottostringa.<br />

${stringa/%sottostringa/sostituto}<br />

Se $sottostringa viene verificata alla fine <strong>di</strong> $stringa, allora $sostituto rimpiazza<br />

$sottostringa.<br />

stringaZ=abcABC123ABCabc<br />

echo ${stringaZ/#abc/XYZ} # XYZABC123ABCabc<br />

# Sostituisce l’occorrenza iniziale ’abc’con’XYZ’.<br />

echo ${stringaZ/%abc/XYZ} # abcABC123ABCXYZ<br />

# Sostituisce l’occorrenza finale ’abc’ con ’XYZ’.<br />

9.2.1. Manipolare stringhe con awk<br />

Uno script <strong>Bash</strong> può ricorrere alle capacità <strong>di</strong> manipolazione delle stringhe <strong>di</strong> awk, come alternativa<br />

all’utilizzo dei propri operatori builtin.<br />

125

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

Saved successfully!

Ooh no, something went wrong!