0Pricing
Linux Command Line Mastery · บทเรียน

การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr

ใช้การแปลงที่ขับเคลื่อนด้วยนิพจน์ปกติกับสตรีมด้วยการแทนที่ของ sed และแปลหรือลบอักขระด้วย tr

การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.conf

Meet 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; -E enables extended regex; \1 reuses captures
  • /re/d and -n '/re/p' select lines
  • sed -i.bak edits files safely
  • tr translates, deletes (-d), and squeezes (-s) characters

คำถามที่พบบ่อย

บทเรียน “การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr”

ใช้การแปลงที่ขับเคลื่อนด้วยนิพจน์ปกติกับสตรีมด้วยการแทนที่ของ sed และแปลหรือลบอักขระด้วย tr คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม

ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เชี่ยวชาญนิพจน์ทั่วไป (Regex)
  2. รูปแบบขั้นสูงของ `grep` และ `find`
  3. การใช้ `xargs` เชื่อมคำสั่ง
  4. การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr
← กลับไปที่ Linux Command Line Mastery