sed是Stream editor的簡稱,和vi不同,sed是行編輯器。sed特點:非互動式修改文本。
sed工作原理
sed是從檔案或管道中讀取一行,處理一行,輸出一行;再讀取一行,再處理一行,再輸出一行,直到最後一行。每當處理一行時,把目前處理的行存儲在臨時緩沖區中,這稱之為模式空間,接着用sed指令處理緩沖區中的内容,處理完成後,把緩沖區的内容送往螢幕。接着處理下一行,這樣不斷重複,直到檔案末尾。
一次處理一行的設計模式使得sed性能很高,sed在讀取大檔案時不會出現卡頓現象。如果使用vi指令打開幾十M上百M的檔案,明顯會出現有卡頓的現象,這是因為vi指令打開檔案時一次性将檔案加載到記憶體,然後再打開。sed就避免了這種情況,一行一行的處理,打開速度非常快,執行速度也很快。
[[email protected] scripts]# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
-b, --binary
does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
open files in binary mode (CR+LFs are not treated specially))
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-s, --separate
consider files as separate rather than as a single,
continuous long stream.
--sandbox
operate in sandbox mode (disable e/r/w commands).
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help
display this help and exit
--version
output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
sed基本用法
sed [option]... 'script;script;...' [inputfile]
sed常用選項
-n 不輸出模式空間内容到螢幕,即不自動列印 -n, --quiet, suppress automatic printing of pattern space
-e 多點編輯
sed輸入啥列印啥

位址格式:
1.不給位址: 對全文進行處理
2.單位址:
#:指定的行, $:最後一行
/pattern/: 被此處模式所能夠比對到的每一行。
3.位址範圍:
#,# 從#行到第#行 3,6從第3行到第6行
#,+# 從#行到+#行 3,+4表示從3行到第7行
/part1/,/part2/
#,/part/
/part/,#
4.步進:~
1~2 奇數行
2~2 偶數行
取出某個檔案中從b開頭的行到s開頭的行
顯示seq 10中的奇數行和偶數行
常用指令
p 列印目前模式空間内容,追加到預設輸出之後
ip 忽略大小寫輸出
d 删除模式空間比對的行,并立即啟用下一輪循環
a 在指定行後面追加文本内容,支援使用\n實作多行追加 a text Append text
i 插入内容(在文本内容的前面加)
c 替換内容單行或者多行 Replace the selected lines with text
w file 挑出指定的内容儲存到别的檔案中
r file 指定内容到指定的比對的行中
-i.bak 備份檔案并原處編輯 (先将原檔案備份為.bak的檔案)
應用:隻列印ip位址子網路遮罩廣播位址
最小化安裝是不支援ifconfig指令的,需要“yum -y install net-tools”
給某個文本裡追加内容
備份原檔案并修改原檔案的某處文本内容
sed -i.bak '/User specific/aalias test=ls' .bashrc
sed '/需要替換的内容/c 替換内容' textfile
修改SElinux為不可用
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
ubuntu開啟root遠端登入功能
#sudo -i
#passwd root
#sed -i '/PermitRootLogin/c PermitRootLogin yes' /etc/ssh/sshd_config
#systemctl restart sshd
#将第3行儲存至目前目錄下的a.txt檔案中
seq 10 | sed '3w ./a.txt'
分組
echo 123456789 |sed -r 's/(123)(456)(789)/\2\1\3/p'
挑出ifconfig ens160的ip位址
ifconfig ens160 | sed -rn '2s#^(.*inet +)([0-9.]+)( +netmask.*)$#\2#p'
修改網卡名“/etc/default/grub”
sed -ri '/GRUB_CMDLINE_LINUX=/s#"$# ifnames=0"#' /etc/default/grub
#修改完後還需要執行以下指令
grub2-mkconfig -o /boot/grub2/grub.cfg ;reboot