sed [OPTION]… {script-only-if-no-other-script} [input-file]…
1 2 3 4 5 6 7 8 9 10 11 12
-n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script addthe script tothe commands to be executed -f script-file, --file=script-file addthe contents of script-filetothe commands to be executed --follow-symlinks follow symlinks when processing in place -i[SUFFIX], --in-place[=SUFFIX] -E, -r, --regexp-extended use extended regular expressions inthe script (for portability use POSIX -E).
xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ cat 10.txt Line 1 192.168.1.1 Line 2 192.168.1.15 Line 3 192.168.1.14 Line 4 192.168.1.17 Line 5 192.168.1.13 Line 6 192.168.1.1 Line 7 192.168.1.12 Line 8 192.168.1.11 Line 9 192.168.1.1 Line 10 192.168.1.18xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ sed 's/Line/line/' 10.txt line 1 192.168.1.1 line 2 192.168.1.15 line 3 192.168.1.14 line 4 192.168.1.17 line 5 192.168.1.13 line 6 192.168.1.1 line 7 192.168.1.12 line 8 192.168.1.11 line 9 192.168.1.1 line 10 192.168.1.18
xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ cat 10.txt Line 1 192.168.1.1 Line 2 192.168.1.15 Line 3 192.168.1.14 Line 4 192.168.1.17 Line 5 192.168.1.13 Line 6 192.168.1.1 Line 7 192.168.1.12 Line 8 192.168.1.11 Line 9 192.168.1.1 Line 10 192.168.1.18 xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ sed '1,2d' 10.txt Line 3 192.168.1.14 Line 4 192.168.1.17 Line 5 192.168.1.13 Line 6 192.168.1.1 Line 7 192.168.1.12 Line 8 192.168.1.11 Line 9 192.168.1.1 Line 10 192.168.1.18
sed a(i)插入
a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行
脚本基本格式:[address]a(或 i)\新文本内容
1 2 3 4 5 6 7 8 9 10 11 12
xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ sed '1a\Line 1.1 11111' 10.txt Line 1 192.168.1.1 Line 1.1 11111 Line 2 192.168.1.15 Line 3 192.168.1.14 Line 4 192.168.1.17 Line 5 192.168.1.13 Line 6 192.168.1.1 Line 7 192.168.1.12 Line 8 192.168.1.11 Line 9 192.168.1.1 Line 10 192.168.1.18
sed c替换
脚本基本格式:[address]c\用于替换的新文本
1 2 3 4 5 6 7 8 9 10 11
xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ sed '1c\Line 1.1 11111' 10.txt Line 1.1 11111 Line 2 192.168.1.15 Line 3 192.168.1.14 Line 4 192.168.1.17 Line 5 192.168.1.13 Line 6 192.168.1.1 Line 7 192.168.1.12 Line 8 192.168.1.11 Line 9 192.168.1.1 Line 10 192.168.1.18
xin@LAPTOP-MRV29Q6U:/mnt/c/Users/YIPING/Desktop$ sed 'y/123/079/' 10.txt Line 0 097.068.0.0 Line 7 097.068.0.05 Line 9 097.068.0.04 Line 4 097.068.0.07 Line 5 097.068.0.09 Line 6 097.068.0.0 Line 7 097.068.0.07 Line 8 097.068.0.00 Line 9 097.068.0.0 Line 00 097.068.0.08
sort 10.txt | uniq -c 2 Line 1 192.168.1.1 1 Line 10 192.168.1.18 1 Line 2 192.168.1.15 1 Line 3 192.168.1.14 1 Line 4 192.168.1.17 1 Line 5 192.168.1.13 1 Line 7 192.168.1.12 1 Line 8 192.168.1.11 1 Line 9 192.168.1.1