الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

cut

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

المستوى
امتحان RHCSA
المجموعة
النصوص والبحث
يحتاج root
لا
الحزمة
coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

cut OPTION... [FILE]...

الخيارات 15

الأكثر استخدامًا

-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.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع 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.

انتبه

  • 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.

نصائح للحفظ

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