🎓 A1Learner
🔍
0 of 50 lessons completed

Cutting Columns

cut takes a slice from each line. A field is one part of a line, split by a mark.

First field of the report

The colon splits each line of report.txt. cut -d: -f1 keeps the first part.

$ cd ~/practice
$ cut -d: -f1 projects/report.txt

Second word of each log line

A space splits each log line. cut -d' ' -f2 keeps the second word.

$ cd ~/practice
$ cut -d' ' -f2 data/logs.txt

First ten letters

cut -c1-10 keeps the first ten letters of each line. Every log line starts with the same date.

$ cd ~/practice
$ cut -c1-10 data/logs.txt

Example

$ cd ~/practice
$ cut -d' ' -f3 data/logs.txt

Key points

  • Use cut to take one part from each line.
  • A delimiter is the mark that splits a line into fields.
  • Use cut -d to name the delimiter, like a colon or a space.
  • Use cut -f to pick a field by its number.
  • Use cut -c to pick letters by their place in the line.
  • cut never changes the file. It only prints the slices.

In this example, cut splits each log line on spaces. -f3 picks the third word. The five third words print, one per line.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which option picks the second field?