هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
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
الأكثر استخدامًا
Commands inside the script
Addresses
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 13
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Replace text in the output
sed 's/http:/https:/g' links.txtThe file does not change; only the output.
2Change a config file in place, with a backup
sed -i.bak 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/configThe old file is kept as config.bak.
3Turn off root login over SSH
sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin no/' /etc/ssh/sshd_configWorks if the line is commented out or not. Then
systemctl reload sshd.4Print only some lines
sed -n '10,20p' /etc/servicesLines 10 to 20.
5Delete comments and empty lines
sed '/^\s*#/d; /^\s*$/d' /etc/chrony.confShows only the active settings.
6Add a line after a match
sed -i '/^\[main\]/a gpgcheck=1' /etc/dnf/dnf.confInserts a line under the [main] section.
7Comment out a line
sed -i 's/^pool /#pool /' /etc/chrony.confPuts
#before every line that starts with "pool ".8Use another separator when the text has slashes
sed 's|/var/www/html|/srv/web|g' site.conf|instead of/keeps it readable.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\3are the parts in brackets.10Print the line numbers of matches
sed -n '/Listen/=' /etc/httpd/conf/httpd.confLike
grep -n, only the numbers.11Delete a line by number
sed -i '3d' ~/.ssh/known_hostsRemoves an old host key (line 3), after "REMOTE HOST IDENTIFICATION HAS CHANGED".
12Replace a value in many files
sed -i 's/old-db.example.com/new-db.example.com/g' /etc/myapp/*.confCheck first with
grep -l old-db /etc/myapp/*.conf.13Mask passwords in a log before you share it
sed -E 's/(password=)[^& ]+/\1*****/g' app.log > app-masked.logNever send real secrets in a support ticket.
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Test without
-ifirst. Then use-i.bakso you can go back. sed -ireplaces the file with a new one (new inode): links and some permissions (and SELinux labels) can change. Check withls -lZon system files.- In basic regex,
+ ? ( ) { } |need a backslash; use-Eto avoid that.
نصائح للحفظ
s/a/b/greplace all,-n 'Np'print line N,/x/ddelete lines with x,-i.bakedit with backup.