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

sed

Change text as it flows through: replace words, delete or print lines — and edit files in place with -i.

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

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

ماذا يفعل

sed (stream editor) reads text line by line, runs editing commands on it, and prints the result. With -i it writes the result back into the file. It is the standard tool to change config files in scripts and automation.

The most used command is substitute: s/OLD/NEW/FLAGS. Commands can be limited to line numbers (5, 5,10, $ = last line) or to lines that match a pattern (/regex/).

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

sed [OPTION]... 'SCRIPT' [FILE...]
sed [OPTION]... -e SCRIPT -e SCRIPT [FILE...]
sed [OPTION]... -f SCRIPT-FILE [FILE...]

الخيارات 35

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

-i[<suffix>], --in-place[=<suffix>]
Edit the file itself. With a suffix (like -i.bak) keep a backup.
-n, --quiet, --silent
Do not print every line; print only with the p command.
-e script, --expression=script
Add a script (use it several times for several commands).
-E, -r, --regexp-extended
Use extended regular expressions (+ ? {} | () without backslashes).
-f file, --file=file
Read the sed commands from a file.
-s, --separate
Treat several files as separate files (not one stream).
-z, --null-data
Lines end with NUL.
-u, --unbuffered
Flush output often (for live pipes).
-l n, --line-length=n
Line wrap length for the l command.
--posix
Turn off GNU extensions.
--sandbox
Do not allow the e, r and w commands.
--debug
Show how sed runs the script.
--help
Show help.
--version
Show the version.

Commands inside the script

s/old/new/
Replace the first match in each line.
s/old/new/g
Replace all matches in each line.
s/old/new/I
Replace, ignoring case (GNU).
s/old/new/2
Replace only the 2nd match.
d
Delete the line.
p
Print the line (with -n).
a text
Append a line after.
i text
Insert a line before.
c text
Change (replace) the whole line.
y/abc/xyz/
Change characters one by one (like tr).
=
Print the line number.
q
Quit.
r file
Read a file in after the line.
w file
Write the line to a file.

Addresses

5
Line 5.
5, 10
Lines 5 to 10.
$
The last line.
/regex/
Lines that match.
/start/, /end/
From a line that matches start to a line that matches end.
addr!
All lines that do NOT match the address.

أمثلة 13

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Replace text in the output

    sed 's/http:/https:/g' links.txt

    The file does not change; only the output.

  2. 2Change a config file in place, with a backup

    sed -i.bak 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config

    The old file is kept as config.bak.

  3. 3Turn off root login over SSH

    sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin no/' /etc/ssh/sshd_config

    Works if the line is commented out or not. Then systemctl reload sshd.

  4. 4Print only some lines

    sed -n '10,20p' /etc/services

    Lines 10 to 20.

  5. 5Delete comments and empty lines

    sed '/^\s*#/d; /^\s*$/d' /etc/chrony.conf

    Shows only the active settings.

  6. 6Add a line after a match

    sed -i '/^\[main\]/a gpgcheck=1' /etc/dnf/dnf.conf

    Inserts a line under the [main] section.

  7. 7Comment out a line

    sed -i 's/^pool /#pool /' /etc/chrony.conf

    Puts # before every line that starts with "pool ".

  8. 8Use another separator when the text has slashes

    sed 's|/var/www/html|/srv/web|g' site.conf

    | instead of / keeps it readable.

  9. 9Use groups to reorder text

    echo "2026-10-25" | sed -E 's/([0-9]+)-([0-9]+)-([0-9]+)/\3.\2.\1/'
    25.10.2026

    \1 \2 \3 are the parts in brackets.

  10. 10Print the line numbers of matches

    sed -n '/Listen/=' /etc/httpd/conf/httpd.conf

    Like grep -n, only the numbers.

  11. 11Delete a line by number

    sed -i '3d' ~/.ssh/known_hosts

    Removes an old host key (line 3), after "REMOTE HOST IDENTIFICATION HAS CHANGED".

  12. 12Replace a value in many files

    sed -i 's/old-db.example.com/new-db.example.com/g' /etc/myapp/*.conf

    Check first with grep -l old-db /etc/myapp/*.conf.

  13. 13Mask passwords in a log before you share it

    sed -E 's/(password=)[^& ]+/\1*****/g' app.log > app-masked.log

    Never send real secrets in a support ticket.

انتبه

  • Test without -i first. Then use -i.bak so you can go back.
  • sed -i replaces the file with a new one (new inode): links and some permissions (and SELinux labels) can change. Check with ls -lZ on system files.
  • In basic regex, + ? ( ) { } | need a backslash; use -E to avoid that.

نصائح للحفظ

  • s/a/b/g replace all, -n 'Np' print line N, /x/d delete lines with x, -i.bak edit with backup.