Linux Command Line Mastery · Lezione

Combinare e ordinare l’output con sort, uniq e cut

Impari a rielaborare e sintetizzare il testo in streaming ordinando le righe, rimuovendo i duplicati ed estraendo i campi con sort, uniq e cut.

Lezione 4 di 413 passaggi

Combinare e ordinare l’output con sort, uniq e cut è una lezione Linux Command Line Mastery gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Linux Command Line Mastery, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Linux Command Line Mastery include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

  • sort orders lines
  • uniq collapses duplicates
  • cut extracts 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' | sort

Numeric 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 -r

Sorting 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 -n

Removing Duplicates with uniq

uniq only collapses adjacent duplicate lines, so you almost always sort first.

printf 'a\na\nb\na\n' | sort | uniq

Counting 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 -c

Top-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 -nr

Extracting 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: -f1

Cutting by Character Position

cut -c selects character ranges instead of fields, useful for fixed-width data.

printf 'ABCDEF\nGHIJKL\n' | cut -c2-4

Finding 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 -d

A 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 -nr

Quick 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 lines
  • uniq (with -c, -d, -u) deduplicates and counts
  • cut (with -d/-f or -c) extracts fields

The cut | sort | uniq -c | sort -nr chain is one of the most useful one-liners in Linux.

Gratis per iniziare

Impara Linux Command Line Mastery con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Combinare e ordinare l’output con sort, uniq e cut» è gratuita?

Sì — il testo completo di «Combinare e ordinare l’output con sort, uniq e cut» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Linux Command Line Mastery, passa a CoddyKit PRO. Il corso Linux Command Line Mastery include 4 lezioni in totale.

Cosa imparerò in «Combinare e ordinare l’output con sort, uniq e cut»?

Impari a rielaborare e sintetizzare il testo in streaming ordinando le righe, rimuovendo i duplicati ed estraendo i campi con sort, uniq e cut. Eserciti Linux Command Line Mastery con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Linux Command Line Mastery?

Non è richiesta alcuna esperienza precedente. Linux Command Line Mastery su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Combinare e ordinare l’output con sort, uniq e cut»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Linux Command Line Mastery?

Sì. Ogni lezione Linux Command Line Mastery include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Flussi standard e redirezione
  2. Filtrare il testo con `grep`
  3. Manipolazione del testo con `sed` e `awk`
  4. Combinare e ordinare l’output con sort, uniq e cut
← Torna a Linux Command Line Mastery