Una Guia Rapida per Usse Sed Commands in Linux

Una introducció à u cumandariu di Linux

U sistema operatore Linux faci u usu di cumandamenti di terminale per travaglià cù un sistema di schedariu d'informàtica. L'usu primariu di u cumandimu Linux , chì hè corta per l'editore stream, hè di mudificà ogni linea di un schedariu o stream per rimpiazzamentu di e parti di a linea. Fai mudificà i cambiamenti di testu in un schedariu o in entru da un pipeline. Per esempiu, dite chì avete un schedariu chjamatu "songs.text" chì cuntene sti linichi:

1, Justin Timberlake, Titulu 545, Prezzo $ 6,30 2, Taylor Swift, Titulu 723, Prezzo $ 7,90 3, Mick Jagger, Titulu 610, Prezzo $ 7,90 4, Lady Gaga, Titulu 118, Prezzo $ 6,30 5, Johnny Cash, Titolu 482, Prezzo $ 6,50 6, Elvis Presley, Titulu 335, Prezzo $ 6,30 7, John Lennon, Titulu 271, Prezzo $ 7,90

Sottuccissioni Testu Sottu Centru

Se vulete cambià tutti l'avvenenzi di u prezzu di $ 6,30 à 7,30 $, pudete fà i cambiune utilizendu l'ordine di sidu in questu modu:

sed's / 6.30 / 7.30 / 'songs.txt> songs2.txt

Stu codice face u cambiamentu è scrive u file mudificatu à "songs2.txt". U schedariu di u contu contene:

1, Justin Timberlake, Titulu 545, Prezzo $ 7,30 2, Taylor Swift, Titulu 723, Prezzo $ 7,90 3, Mick Jagger, Titulu 610, Prezzo $ 7,90 4, Lady Gaga, Titulu 118, Prezzo $ 7,30 5, Johnny Cash, Titulu 482, Prezzo $ 6,50 6, Elvis Presley, Titulu 335, Prezzo $ 7,30 7, John Lennon, Titulu 271, Prezzo $ 7,90

Sè vo vulete sustituiri tutti l'azzioni di "Cash" cù "Trash" pudete aduprà:

sed / Cash / Trash / 'songs.txt> songs2.txt

chì crea un schedariu cù u cuntenutu:

1, Justin Timberlake, Titulu 545, Prezzo $ 7: 30 2, Taylor Swift, Titulu 723, Prezzo $ 7,90 3, Mick Jagger, Titulu 610, Prezzo $ 7,90 4, Lady Gaga, Titulu 118, Prezzo $ 7: 30 5, Johnny Trash, Titolu 482, Prezzo $ 6,50 6, Elvis Presley, Titulu 335, Prezzo $ 7: 30 7, John Lennon, Titulu 271, Prezzo $ 7,90

I filtrazione cù u Sed Command

Sed hè ancu spessu usatu per filtre lie in un schedariu o flussu. Per esempiu, sè vo vulete vede i linii chì cuntenenu "Ghjuvanni", pudete aduprà:

sed -n '/ John / p' songs.txt> johns.txt

chì scrive e seguenti linee per u file johns.txt:

5, Johnny Trash, Titulu 482, Prezzo $ 6,50 7, John Lennon, Titulu 271, Prezzo $ 7,90