← Linux and the Command Line Lesson 23 of 51 · 10 XP

cut — cutting out columns

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

cut cuts out parts of lines — by a separator or by character positions:

cut -d, -f2 data.csv            # the second field, the separator is a comma
cut -d, -f1,3 data.csv          # the first and the third
cut -d: -f1 /etc/passwd         # the names of all the users on the system
cut -c1-10 file                 # characters 1 to 10

cut is simpler and faster than awk, but it can do less. For example, it treats several spaces in a row as several separators. For tables aligned with spaces, awk '{ print $2 }' is better.

/etc/passwd is a classic example. In it each line is a user, and the fields are separated by colons:

kai:x:1000:1000:Kai:/home/kai:/bin/zsh
name:password:UID:GID:description:home_folder:shell

Task. The input is lines in the /etc/passwd format. Print, separated by a space, the user's name and their shell (the 1st and 7th fields) — only for those whose shell is /bin/bash.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.