使用 sed 与 tr 进行大规模流编辑
使用由正则表达式驱动的 sed 替换跨流执行转换,并使用 tr 转换或删除字符。
使用 sed 与 tr 进行大规模流编辑 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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
常见问题解答
「使用 sed 与 tr 进行大规模流编辑」课时是免费的吗?
是的 — 「使用 sed 与 tr 进行大规模流编辑」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「使用 sed 与 tr 进行大规模流编辑」这节课中我会学到什么?
使用由正则表达式驱动的 sed 替换跨流执行转换,并使用 tr 转换或删除字符。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 sed 与 tr 进行大规模流编辑」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 掌握正则表达式(Regex)
- 高级 `grep` 和 `find` 模式
- 使用 `xargs` 链接命令
- 使用 sed 与 tr 进行大规模流编辑