การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut
เรียนรู้การปรับรูปแบบและสรุปข้อความที่ไหลผ่านด้วยการเรียงบรรทัด ลบข้อมูลซ้ำ และดึงฟิลด์ด้วย sort, uniq และ cut
การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why sort, uniq, and cut Matter
Filtering with grep and editing with sed/awk gets you far, but real pipelines often need to reorder, deduplicate, and slice columns of text.
sortorders linesuniqcollapses duplicatescutextracts fields
Together they turn raw streams into clean summaries.
Basic Sorting
sort reads lines and prints them in lexical order by default. It works great inside a pipe.
printf 'banana\napple\ncherry\n' | sortNumeric and Reverse Sort
Use -n for numeric order and -r to reverse. Without -n, 10 sorts before 2.
printf '10\n2\n33\n4\n' | sort -n -rSorting by a Specific Field
-k picks the column to sort on and -t sets the delimiter. Here we sort a CSV by the second field numerically.
printf 'alice,30\nbob,25\ncarol,40\n' | sort -t, -k2 -nRemoving Duplicates with uniq
uniq only collapses adjacent duplicate lines, so you almost always sort first.
printf 'a\na\nb\na\n' | sort | uniqCounting Occurrences
uniq -c prefixes each line with how many times it appeared. This is the classic frequency-count idiom.
printf 'err\nok\nerr\nerr\nok\n' | sort | uniq -cTop-N Frequency Report
Combine the tools to build a leaderboard: count, then sort the counts descending.
printf 'GET\nPOST\nGET\nGET\nPOST\n' | sort | uniq -c | sort -nrExtracting Columns with cut
cut -d sets the delimiter and -f selects fields. Here we grab the username column from a colon-separated record.
printf 'root:x:0\nalice:x:1000\n' | cut -d: -f1Cutting by Character Position
cut -c selects character ranges instead of fields, useful for fixed-width data.
printf 'ABCDEF\nGHIJKL\n' | cut -c2-4Finding Only Unique or Only Duplicated Lines
uniq -u prints lines that appear exactly once; uniq -d prints only those that repeat.
printf 'x\ny\nx\nz\n' | sort | uniq -dA Full Pipeline
Put it all together: extract a field, sort it, count duplicates, and rank. This pattern answers questions like which IP hit us most?
printf '1.1.1.1 GET\n2.2.2.2 GET\n1.1.1.1 POST\n' | cut -d' ' -f1 | sort | uniq -c | sort -nrQuick Check
Why must you usually run sort before uniq?
Recap
You can now reshape text streams end to end:
sort(with-n,-r,-k,-t) orders linesuniq(with-c,-d,-u) deduplicates and countscut(with-d/-for-c) extracts fields
The cut | sort | uniq -c | sort -nr chain is one of the most useful one-liners in Linux.
เรียนรู้ Linux Command Line Mastery ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut”
เรียนรู้การปรับรูปแบบและสรุปข้อความที่ไหลผ่านด้วยการเรียงบรรทัด ลบข้อมูลซ้ำ และดึงฟิลด์ด้วย sort, uniq และ cut คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม
ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สตรีมมาตรฐานและการเปลี่ยนเส้นทาง
- การกรองข้อความด้วย `grep`
- การจัดการข้อความด้วย `sed` และ `awk`
- การรวมและเรียงลำดับผลลัพธ์ด้วย sort, uniq และ cut