Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

cut

Take parts of each line: fields split by a separator, or character positions.

Niveau
RHCSA-examen
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

cut prints selected parts of every line. With -d you give the separator (one character) and with -f the field numbers. With -c you select characters by position.

It is ideal for files with a fixed separator like /etc/passwd (:) or CSV (,). For columns separated by a varying number of spaces, awk is better.

Zo schrijf je het

cut OPTION... [FILE]...

Opties 15

Meest gebruikt

-f, --fields=list
Select these fields, like 1,3 or 2-4 or 3-.
-d, --delimiter=char
The field separator (default: TAB).
-c, --characters=list
Select these character positions.
-b, --bytes=list
Select these byte positions.
-s, --only-delimited
Do not print lines that have no separator.
--complement
Print everything except the selected parts.
--output-delimiter=string
Use this separator in the output.
-z, --zero-terminated
Lines end with NUL.
-n
Ignored.
--help
Show help.
--version
Show the version.

LIST forms

N
Only field (or character) N.
N-M
From N to M.
N-
From N to the end of the line.
-M
From the first to M.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1All user names

    cut -d: -f1 /etc/passwd

    Field 1 of /etc/passwd is the user name.

  2. 2User name and shell

    cut -d: -f1,7 /etc/passwd
    root:/bin/bash

    Fields 1 and 7, still joined by :.

  3. 3The first 8 characters of each line

    cut -c1-8 /etc/passwd

    Character positions, not fields.

  4. 4Columns of a CSV file

    cut -d, -f2,4 users.csv

    Fields 2 and 4 (note: quoted commas inside fields break this; use a CSV tool then).

  5. 5Everything except field 2

    cut -d: -f2 --complement /etc/passwd

    Removes the password placeholder column.

  6. 6Change the output separator

    cut -d: -f1,6 --output-delimiter=' -> ' /etc/passwd
    root -> /root

    Readable output for a report.

  7. 7Only the group names

    cut -d: -f1 /etc/group | sort

    All local groups, sorted.

  8. 8Get the IP address part of an address/prefix

    ip -4 -o addr show dev enp1s0 | awk '{print $4}' | cut -d/ -f1
    192.168.122.10

    cut -d/ -f1 removes the /24.

  9. 9Field 3 to the end

    cut -d' ' -f3- server.log

    Drops the date and time columns of a log line.

  10. 10Skip lines without the separator

    cut -d= -f2 -s app.conf

    Comment lines without = are not printed.

  11. 11The size column of a TAB separated report

    cut -f2 report.tsv | sort -n | tail -1

    TAB is the default separator.

Let op

  • cut uses exactly one separator character. Many spaces in a row make empty fields; use awk or tr -s ' ' first.
  • Field numbers start at 1.

Tips om te onthouden

  • cut -d: -f1 = first column of passwd-style files.