Reshape text

INPUT · Slides

Pull out a column (`cut`)

01 / 06

You only want part of the line

In the last lesson you counted addresses in a log. But that was a file of nothing but addresses. Real logs have long lines.

192.168.0.1 - - [21/Aug/2026] "GET /index.html" 200

When you want to count only the address in there, handing over whole lines is no use. Every line differs, so uniq -c answers "one" for all of them.

You need a tool that pulls out just the column you want. That is cut.

02 / 06

Name the separator and the column number

You tell cut two things.

  • -d — what the separator (delimiter) is
  • -f — which column (field) you want
cut -d, -f1 grades.csv

"Split on commas and give me column 1."

Without -d the separator is a tab. For tab separated files, -f on its own is enough.

~ $ cut -d, -f1 grades.csvtanakasuzukisato

03 / 06

Several columns, and ranges

You are not limited to one column.

How you write itMeaning
-f1column 1
-f1,3columns 1 and 3
-f2-column 2 to the end
-f1-3columns 1 to 3

When you take several columns, the separator stays in. cut -d, -f1,3 gives you tanaka,3, comma and all.

04 / 06

You can also cut by character count

For data with no separator, -c (character) lets you name which characters.

cut -c1-3 letters.txt    # characters 1 to 3cut -c5 letters.txt      # character 5 onlycut -c3- letters.txt     # character 3 to the end

This is for data laid out in fixed widths (fixed length, it is called). You still see it in data from banks and government offices.

Be careful with characters outside plain English. -c counts characters and -b counts bytes, so -b can cut a character in half and break it.

05 / 06

Know where cut gives up

There are two things it cannot do, and they catch you out if you do not know.

1. It cannot reorder the columns.

~ $ cut -d, -f3,1 grades.csvtanaka,3

Write -f3,1 and you still do not get them in 3,1 order; they always come out lowest number first.

2. It breaks when the spacing is uneven.

With tanaka 80, three spaces in a row, -d" " counts "column 1 = tanaka, column 2 = empty, column 3 = empty..." because each separator counts one at a time.

When you need either of those, you use awk, which comes in the second half of this chapter.

06 / 06

Now have a go

Four files.

  • grades.csv — three lines of name,score,rank
  • pw.txt — the same shape as /etc/passwd (: separated, seven columns)
  • letters.txt — two ten character lines such as abcdefghij
  • gaps.txttanaka 80 (three spaces) and suzuki 95 (one space)

gaps.txt is deliberately broken data, put there so you can watch it fail. Failing once means you will spot the same situation when you meet it later.