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

การจัดการข้อความด้วย `sed` และ `awk`

ใช้ `sed` สำหรับแก้ไขสตรีม และ `awk` สำหรับประมวลผลกับสร้างรายงานข้อความขั้นสูง

การจัดการข้อความด้วย `sed` และ `awk` เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Meet `sed` and `awk`

Welcome to a powerful lesson on text manipulation! We'll explore two essential Linux command-line tools: sed and awk.

These tools are incredibly useful for editing and processing text files, making them indispensable for system administrators, developers, and data analysts.

  • sed (Stream Editor): Great for simple text transformations like finding and replacing text or deleting lines.
  • awk (Pattern Scanning and Processing Language): A more robust language for advanced data extraction, reporting, and conditional processing.

`sed` for Simple Substitutions

The most common use for sed is substitution. You can replace specific text patterns on a line using the s command, which stands for substitute.

The basic syntax is sed 's/pattern/replacement/'. By default, it replaces only the first occurrence of the pattern on each line.

echo "hello world, hello universe" | sed 's/hello/hi/'

Global & Case-Insensitive `sed`

What if you want to replace all occurrences on a line, or ignore case? sed has flags for that!

  • The g flag (global) replaces all matches on a line.
  • The i flag (case-insensitive) makes the pattern matching ignore uppercase/lowercase differences.

You can combine flags, like gi.

echo "Apple apple APPLE" | sed 's/apple/orange/gi'

Deleting Lines with `sed`

Beyond substitution, sed can also delete entire lines based on a pattern or line number. The d command is used for deletion.

To delete lines containing a specific word, you'd use sed '/pattern/d'. You can also delete specific line numbers, like sed '2d' for the second line.

echo -e "Line 1
REMOVE THIS LINE
Line 3" | sed '/REMOVE/d'

Inserting & Appending Lines

sed can also insert new lines of text before or after existing lines. This is useful for adding headers, footers, or context to your files.

  • The i command inserts text before a matched line.
  • The a command appends text after a matched line.

The text to insert/append must be on a new line after the command, or escaped with a backslash.

echo "Existing line" | sed 'i\New line before'

`awk` for Pattern Scanning

Now, let's dive into awk. It's more than just a stream editor; it's a programming language designed for pattern scanning and processing!

awk processes text files line by line. For each line, it checks if it matches a given pattern. If it does, it performs an associated action. The basic structure is awk 'pattern { action }'.

echo -e "apple
banana
orange" | awk '{ print }'

Working with `awk` Fields

A key feature of awk is its ability to automatically split each line into fields (columns). By default, whitespace (spaces, tabs) acts as the field separator.

  • $1, $2, $3, ...: Refer to the first, second, third field, and so on.
  • $0: Refers to the entire line.
  • -F option: Use this to specify a custom field separator, e.g., -F',' for comma-separated values.
echo "Name:Alice,Age:30" | awk -F',' '{ print "Hello, " $1 }'

Conditional Logic in `awk`

awk allows you to use conditional statements, like if, within its actions. This means you can process lines or fields only if certain criteria are met.

This is extremely powerful for filtering data based on numeric comparisons, string matches, and more.

echo -e "itemA 10
itemB 5
itemC 12" | awk '{ if ($2 > 8) print $1 " is expensive" }'

`awk`'s Handy Variables

awk provides several built-in variables that offer useful information during processing:

  • NR (Number of Record): The current line number being processed.
  • NF (Number of Fields): The total number of fields in the current line.
  • FILENAME: The name of the current input file.

These variables help you create dynamic and context-aware scripts.

echo -e "one two
three four five" | awk '{ print "Line " NR ": " NF " fields" }'

`sed` Substitution Quiz

Time to test your knowledge of sed!

`sed` & `awk` Recap

Great job! You've learned the basics of two incredibly powerful text processing tools: sed and awk.

  • sed is your go-to for stream editing, perfect for quick substitutions (s), deleting lines (d), and inserting/appending text. Remember the g (global) and i (case-insensitive) flags!
  • awk is a versatile language for pattern scanning. It excels at splitting lines into fields ($1, $2, ...), using custom delimiters (-F), applying conditional logic (if), and leveraging built-in variables like NR and NF.

Practice these commands to master your text manipulation skills!

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

บทเรียน “การจัดการข้อความด้วย `sed` และ `awk`” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อความด้วย `sed` และ `awk`”

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

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

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

บทเรียน “การจัดการข้อความด้วย `sed` และ `awk`” ใช้เวลานานแค่ไหน

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

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

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

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

  1. สตรีมมาตรฐานและการเปลี่ยนเส้นทาง
  2. การกรองข้อความด้วย `grep`
  3. การจัดการข้อความด้วย `sed` และ `awk`
  4. การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut
← กลับไปที่ Linux Command Line Mastery