sort、uniq、cut で出力を結合・並べ替える
sort、uniq、cut を使い、行の並べ替え、重複の削除、フィールドの抽出によって、ストリーム形式のテキストを整形・要約する方法を学びます。
「sort、uniq、cut で出力を結合・並べ替える」はCoddyKit上の無料Linux Command Line Masteryレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.
よくある質問
「sort、uniq、cut で出力を結合・並べ替える」レッスンは無料ですか?
はい。「sort、uniq、cut で出力を結合・並べ替える」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Linux Command Line Masteryコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Linux Command Line Masteryコースには全4レッスンが含まれています。
「sort、uniq、cut で出力を結合・並べ替える」で何を学びますか?
sort、uniq、cut を使い、行の並べ替え、重複の削除、フィールドの抽出によって、ストリーム形式のテキストを整形・要約する方法を学びます。 ブラウザで直接実行するハンズオンコードでLinux Command Line Masteryを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Linux Command Line Masteryを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのLinux Command Line Masteryは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「sort、uniq、cut で出力を結合・並べ替える」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このLinux Command Line Masteryレッスンでコードを書いて実行できますか?
はい。すべてのLinux Command Line Masteryレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 標準ストリームとリダイレクト
- `grep`によるテキストの絞り込み
- `sed`と`awk`によるテキスト操作
- sort、uniq、cut で出力を結合・並べ替える