Modificare stream su larga scala con sed e tr
Applichi trasformazioni basate sulle espressioni regolari agli stream usando la sostituzione di sed e traduca o elimini caratteri con tr.
Modificare stream su larga scala con sed e tr è una lezione Linux Command Line Mastery gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Linux Command Line Mastery, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Linux Command Line Mastery include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
From Matching to Transforming
You have mastered regex matching with grep and find. The natural next step is transforming the text you match. sed and tr turn your regex skills into edits.
sed Substitution Basics
The s/pattern/replacement/ command replaces the first match per line. Add g for all matches.
echo 'cat cat cat' | sed 's/cat/dog/g'Using Regex in sed
sed -E enables extended regex so you can use +, ?, and groups without backslashes.
echo 'a123b' | sed -E 's/[0-9]+/#/'Capture Groups and Backreferences
Parentheses capture parts of the match; \1, \2 reuse them in the replacement. Here we swap two words.
echo 'John Smith' | sed -E 's/(\w+) (\w+)/\2 \1/'Anchoring Substitutions
Use ^ and $ to act only at the start or end of a line, e.g. prefixing every line.
printf 'one\ntwo\n' | sed 's/^/> /'Deleting and Selecting Lines
sed can target lines by pattern: /regex/d deletes matching lines, while -n '/regex/p' prints only matches.
printf 'keep\ndrop me\nkeep\n' | sed '/drop/d'In-Place Editing
sed -i edits a file directly. Always keep a backup with -i.bak until you trust the command.
sed -i.bak 's/localhost/127.0.0.1/g' config.confMeet tr
tr translates or deletes characters (not regex). It reads stdin only. Here we uppercase text.
echo 'hello' | tr 'a-z' 'A-Z'Deleting and Squeezing
tr -d removes characters and tr -s squeezes repeats into one, great for cleaning whitespace.
echo 'a b c' | tr -s ' 'Combining sed and tr
Chain them: normalize a CSV by converting commas to newlines and stripping carriage returns.
echo 'a,b,c' | tr ',' '\n' | sed 's/^/item: /'When to Use Which
Reach for sed when you need patterns and line logic; reach for tr for fast single-character translations and deletions.
Quick Check
You want to replace every occurrence of a word on each line with sed. What must you add?
Recap
You can now transform streams with regex:
sed 's///g'substitutes;-Eenables extended regex;\1reuses captures/re/dand-n '/re/p'select linessed -i.bakedits files safelytrtranslates, deletes (-d), and squeezes (-s) characters
Domande Frequenti
La lezione «Modificare stream su larga scala con sed e tr» è gratuita?
Sì — il testo completo di «Modificare stream su larga scala con sed e tr» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Linux Command Line Mastery, passa a CoddyKit PRO. Il corso Linux Command Line Mastery include 4 lezioni in totale.
Cosa imparerò in «Modificare stream su larga scala con sed e tr»?
Applichi trasformazioni basate sulle espressioni regolari agli stream usando la sostituzione di sed e traduca o elimini caratteri con tr. Eserciti Linux Command Line Mastery con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Linux Command Line Mastery?
Non è richiesta alcuna esperienza precedente. Linux Command Line Mastery su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Modificare stream su larga scala con sed e tr»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Linux Command Line Mastery?
Sì. Ogni lezione Linux Command Line Mastery include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Padroneggiare le espressioni regolari (regex)
- Pattern avanzati con `grep` e `find`
- `xargs` per concatenare i comandi
- Modificare stream su larga scala con sed e tr