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.txtSecond 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.txtFirst 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.txtExample
$ cd ~/practice
$ cut -d' ' -f3 data/logs.txtKey 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?