sed と tr による大規模なストリーム編集
sed の置換を使って正規表現ベースの変換をストリーム全体に適用し、tr で文字を変換または削除する方法を学びます。
「sed と tr による大規模なストリーム編集」はCoddyKit上の無料Linux Command Line Masteryレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 による大規模なストリーム編集」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Linux Command Line Masteryコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Linux Command Line Masteryコースには全4レッスンが含まれています。
「sed と tr による大規模なストリーム編集」で何を学びますか?
sed の置換を使って正規表現ベースの変換をストリーム全体に適用し、tr で文字を変換または削除する方法を学びます。 ブラウザで直接実行するハンズオンコードでLinux Command Line Masteryを演習し、24時間対応の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 による大規模なストリーム編集