CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Text Processing / cut

cut

Cut columns or character ranges out of each line

Beginnercolumnscsvpipelines

cut prints selected parts of every line: fields split on a single-character delimiter (-d, -f) or fixed character positions (-c). It's the quickest way to take usernames from /etc/passwd, a column from a CSV, or the timestamp prefix from log lines.

Syntaxcut -d DELIM -f LIST [FILE]... | cut -c LIST [FILE]...

Watch it run

During an access review you need each account's login shell, and the username and team columns from an HR export.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
cut -d: -f1,7 /etc/passwd | tail -4

/etc/passwd has 7 colon-separated fields. -f1,7 keeps the username and the shell, joined with the same delimiter. nologin means a service account nobody can log in as.

Anatomy of a command

$
commandcut

Selects parts of each line. It never reorders: fields always come out in their original order.

How it works

cut reads its input line by line and, for each line, walks the bytes once. In field mode it counts delimiter characters: every single occurrence of the delimiter starts a new field, so two spaces in a row mean an empty field between them. That one rule explains most cut surprises. Lines that don't contain the delimiter at all are printed whole unless you add -s. In character mode (-c) it simply keeps the byte positions you list; GNU cut treats -c like -b (bytes), so multi-byte UTF-8 characters can be split. cut never parses quotes, so a CSV value like "Lopez, Maria" counts as two fields. It holds no state between lines and starts instantly, which makes it ideal inside large pipelines when the data really is cleanly delimited.

Options you'll actually use

-d CField delimiter (a single character); default is TAB
-f LISTFields to print: 1,3 or 2-4 or 3- (to the end) or -2 (up to 2)
-c LISTCharacter positions to print, e.g. -c1-10
-b LISTByte positions (same as -c in GNU cut)
-sSkip lines that don't contain the delimiter (headers, comments)
--complementPrint everything except the selected fields
--output-delimiter=STRJoin output fields with STR instead of the input delimiter

Common mistakes

ps aux | cut -d' ' -f2 to get PIDs

ps right-aligns columns with a variable number of spaces, so the 'second field' is usually empty.

ps aux | awk '{print $2}' # or: ps -eo pid=
cut -f2 servers.txt on a space-separated file

The default delimiter is TAB. Lines without a tab are printed whole, so it looks like cut did nothing.

cut -d' ' -f2 servers.txt
cut -d, -f3,1 users.csv expecting field 3 before field 1

cut always prints fields in their input order, whatever order you list them in.

awk -F, -v OFS=, '{print $3, $1}' users.csv
cut -d= -f2 app.env to read values

Values that contain = (base64 secrets, URLs with query strings) are cut at their first =.

cut -d= -f2- app.env

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print only the usernames from /etc/passwd.

  2. 2

    From ~/data/users.csv, print the email column without the header row.

  3. 3

    Print the first 19 characters (date and time) of the last 5 lines of ~/logs/app.log.

  4. 4

    Show the IP address column of ~/data/servers.txt even though fields are separated by spaces.

Related commands

awkProcess text by columns: filter, sum, count and reporttrTranslate, squeeze or delete characterssortSort lines: alphabetically, numerically, by columnpasteMerge lines of files side by side, or join lines into one
PreviousawkNextsort
Linux / Text Processing / cut

cut

Cut columns or character ranges out of each line

Beginnercolumnscsvpipelines

cut prints selected parts of every line: fields split on a single-character delimiter (-d, -f) or fixed character positions (-c). It's the quickest way to take usernames from /etc/passwd, a column from a CSV, or the timestamp prefix from log lines.

Syntaxcut -d DELIM -f LIST [FILE]... | cut -c LIST [FILE]...

Watch it run

During an access review you need each account's login shell, and the username and team columns from an HR export.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
cut -d: -f1,7 /etc/passwd | tail -4

/etc/passwd has 7 colon-separated fields. -f1,7 keeps the username and the shell, joined with the same delimiter. nologin means a service account nobody can log in as.

Anatomy of a command

$
commandcut

Selects parts of each line. It never reorders: fields always come out in their original order.

How it works

cut reads its input line by line and, for each line, walks the bytes once. In field mode it counts delimiter characters: every single occurrence of the delimiter starts a new field, so two spaces in a row mean an empty field between them. That one rule explains most cut surprises. Lines that don't contain the delimiter at all are printed whole unless you add -s. In character mode (-c) it simply keeps the byte positions you list; GNU cut treats -c like -b (bytes), so multi-byte UTF-8 characters can be split. cut never parses quotes, so a CSV value like "Lopez, Maria" counts as two fields. It holds no state between lines and starts instantly, which makes it ideal inside large pipelines when the data really is cleanly delimited.

Options you'll actually use

-d CField delimiter (a single character); default is TAB
-f LISTFields to print: 1,3 or 2-4 or 3- (to the end) or -2 (up to 2)
-c LISTCharacter positions to print, e.g. -c1-10
-b LISTByte positions (same as -c in GNU cut)
-sSkip lines that don't contain the delimiter (headers, comments)
--complementPrint everything except the selected fields
--output-delimiter=STRJoin output fields with STR instead of the input delimiter

Common mistakes

ps aux | cut -d' ' -f2 to get PIDs

ps right-aligns columns with a variable number of spaces, so the 'second field' is usually empty.

ps aux | awk '{print $2}' # or: ps -eo pid=
cut -f2 servers.txt on a space-separated file

The default delimiter is TAB. Lines without a tab are printed whole, so it looks like cut did nothing.

cut -d' ' -f2 servers.txt
cut -d, -f3,1 users.csv expecting field 3 before field 1

cut always prints fields in their input order, whatever order you list them in.

awk -F, -v OFS=, '{print $3, $1}' users.csv
cut -d= -f2 app.env to read values

Values that contain = (base64 secrets, URLs with query strings) are cut at their first =.

cut -d= -f2- app.env

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print only the usernames from /etc/passwd.

  2. 2

    From ~/data/users.csv, print the email column without the header row.

  3. 3

    Print the first 19 characters (date and time) of the last 5 lines of ~/logs/app.log.

  4. 4

    Show the IP address column of ~/data/servers.txt even though fields are separated by spaces.

Related commands

awkProcess text by columns: filter, sum, count and reporttrTranslate, squeeze or delete characterssortSort lines: alphabetically, numerically, by columnpasteMerge lines of files side by side, or join lines into one
PreviousawkNextsort