Sikau

Linux and the shell · Day 8 · about 30 min

Slicing and counting

cut, sort, uniq, tr, head and wc in pipelines.

Spreadsheet-free answers

"Which category has the most dishes?" "Top 3 visitors?" — chains of cut, sort and uniq -c answer these in one line, on files far too big for a spreadsheet.

The classic pipeline

cut -d, -f1,3 menu.csv | head -4
tail -n +2 menu.csv | cut -d, -f2 | sort | uniq -c | sort -rn
tail -n +2 menu.csv | sort -t, -k3 -n | head -3
tail -n +2 menu.csv | cut -d, -f1 | tr a-z A-Z | head -3
echo "a,b,,c" | tr -s ,
tail -n +2 menu.csv | cut -d, -f2 | sort -u

The rest of this lesson

  • Sorting numbers and columnsexplain
  • The counting pipelineexplain
  • Why sort first?predict
  • Top categoriesorder
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 16 lessons of Linux and the shell