sedコマンドツールの使い方

王林
リリース: 2024-04-13 13:40:01
転載
783 人が閲覧しました

sedコマンドツールの使い方

sed用法

使用sed可以实现grep的大部分功能,而且还可以查找替换。

语法:

sed [options] 'command' filename

sed [options] -f scriptfile filename

Options:

-n:-n选项和p命令一起使用表示打印

-e:等价于--expression,多点编辑,在一行执行多条命令

用sed实现匹配(grep)功能=打印有root字符串的行

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -n '/root/'p test.txt </span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -nr '/r+o/'p test.txt </span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -nr '/o*t/'p test.txt </span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">shutdown:</span><span class="hljs-symbol">x:</span><span class="hljs-number">6</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:shutdown</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/shutdown</span>
<span class="hljs-symbol">halt:</span><span class="hljs-symbol">x:</span><span class="hljs-number">7</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:halo1t</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/halt</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">ftp:</span><span class="hljs-symbol">x:</span><span class="hljs-number">14</span><span class="hljs-symbol">:</span><span class="hljs-number">50</span><span class="hljs-symbol">:FTP</span> <span class="hljs-symbol">User:</span>/var/<span class="hljs-symbol">ftp:</span>/sbin/nologin
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
systemd-<span class="hljs-symbol">network:</span><span class="hljs-symbol">x:</span><span class="hljs-number">192</span><span class="hljs-symbol">:</span><span class="hljs-number">192</span><span class="hljs-symbol">:systemd</span> Network <span class="hljs-symbol">Management:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">dbus:</span><span class="hljs-symbol">x:</span><span class="hljs-number">81</span><span class="hljs-symbol">:</span><span class="hljs-number">81</span><span class="hljs-symbol">:System</span> message <span class="hljs-symbol">bus:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">polkitd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">998</span><span class="hljs-symbol">:</span><span class="hljs-number">996</span><span class="hljs-symbol">:User</span> <span class="hljs-keyword">for</span> <span class="hljs-symbol">polkitd:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin

ログイン後にコピー

忽略大小写(I)

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -n '/nologin/'Ip test.txt </span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">daemon:</span><span class="hljs-symbol">x:</span><span class="hljs-number">2</span><span class="hljs-symbol">:</span><span class="hljs-number">2</span><span class="hljs-symbol">:daemon</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">ad11om:</span><span class="hljs-symbol">x:</span><span class="hljs-number">3</span><span class="hljs-symbol">:</span><span class="hljs-number">4</span><span class="hljs-symbol">:ad1om</span><span class="hljs-symbol">:/var/adm</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">lp:</span><span class="hljs-symbol">x:</span><span class="hljs-number">4</span><span class="hljs-symbol">:</span><span class="hljs-number">7</span><span class="hljs-symbol">:lp</span><span class="hljs-symbol">:/var/spoool/lpd</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">mail:</span><span class="hljs-symbol">x:</span><span class="hljs-number">8</span><span class="hljs-symbol">:</span><span class="hljs-number">12</span><span class="hljs-symbol">:mail</span><span class="hljs-symbol">:/var/spoooooool/mail</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">games:</span><span class="hljs-symbol">x:</span><span class="hljs-number">12</span><span class="hljs-symbol">:</span><span class="hljs-number">100</span><span class="hljs-symbol">:games</span><span class="hljs-symbol">:/usr/games</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">ftp:</span><span class="hljs-symbol">x:</span><span class="hljs-number">14</span><span class="hljs-symbol">:</span><span class="hljs-number">50</span><span class="hljs-symbol">:FTP</span> <span class="hljs-symbol">User:</span>/var/<span class="hljs-symbol">ftp:</span>/sbin/nologin
<span class="hljs-symbol">nobody:</span><span class="hljs-symbol">x:</span><span class="hljs-number">99</span><span class="hljs-symbol">:</span><span class="hljs-number">99</span><span class="hljs-symbol">:Nobody</span><span class="hljs-symbol">:/</span><span class="hljs-symbol">:/sbin/nologin</span>
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
systemd-<span class="hljs-symbol">network:</span><span class="hljs-symbol">x:</span><span class="hljs-number">192</span><span class="hljs-symbol">:</span><span class="hljs-number">192</span><span class="hljs-symbol">:systemd</span> Network <span class="hljs-symbol">Management:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">dbus:</span><span class="hljs-symbol">x:</span><span class="hljs-number">81</span><span class="hljs-symbol">:</span><span class="hljs-number">81</span><span class="hljs-symbol">:System</span> message <span class="hljs-symbol">bus:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">polkitd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">998</span><span class="hljs-symbol">:</span><span class="hljs-number">996</span><span class="hljs-symbol">:User</span> <span class="hljs-keyword">for</span> <span class="hljs-symbol">polkitd:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>
ログイン後にコピー

打印指定行(p)

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -n '2'p test.txt</span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -n '2,5'p test.txt</span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>
<span class="hljs-number">222222</span><span class="hljs-symbol">:r</span>.<span class="hljs-symbol">o:</span>dfdsfdsg
<span class="hljs-symbol">daemon:</span><span class="hljs-symbol">x:</span><span class="hljs-number">2</span><span class="hljs-symbol">:</span><span class="hljs-number">2</span><span class="hljs-symbol">:daemon</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/nologin</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -n '15,$'p test.txt</span>
<span class="hljs-symbol">nobody:</span><span class="hljs-symbol">x:</span><span class="hljs-number">99</span><span class="hljs-symbol">:</span><span class="hljs-number">99</span><span class="hljs-symbol">:Nobody</span><span class="hljs-symbol">:/</span><span class="hljs-symbol">:/sbin/nologin</span>
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
systemd-<span class="hljs-symbol">network:</span><span class="hljs-symbol">x:</span><span class="hljs-number">192</span><span class="hljs-symbol">:</span><span class="hljs-number">192</span><span class="hljs-symbol">:systemd</span> Network <span class="hljs-symbol">Management:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">dbus:</span><span class="hljs-symbol">x:</span><span class="hljs-number">81</span><span class="hljs-symbol">:</span><span class="hljs-number">81</span><span class="hljs-symbol">:System</span> message <span class="hljs-symbol">bus:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">polkitd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">998</span><span class="hljs-symbol">:</span><span class="hljs-number">996</span><span class="hljs-symbol">:User</span> <span class="hljs-keyword">for</span> <span class="hljs-symbol">polkitd:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>
ログイン後にコピー

-e选项允许在同一行里执行多条命令(把第一行打印出来并匹配bus)

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -e '1'p -e '/bus/'p -n test.txt</span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">dbus:</span><span class="hljs-symbol">x:</span><span class="hljs-number">81</span><span class="hljs-symbol">:</span><span class="hljs-number">81</span><span class="hljs-symbol">:System</span> message <span class="hljs-symbol">bus:</span>/<span class="hljs-symbol">:/sbin/nologin</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -e '1'p -e '/root/'p -n test.txt </span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>
ログイン後にコピー

说明 “sed -e '1'p -e '/bus/'p -n test.txt”该命令的意思是:打印第一行,同时也打印出匹配有‘bus’的行。当打印的内容和匹配的内容相同时,会分别显示,即:两个命令同时进行但相对独立。

删除指定行(d)

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed '1,10'd test.txt</span>
<span class="hljs-symbol">mail:</span><span class="hljs-symbol">x:</span><span class="hljs-number">8</span><span class="hljs-symbol">:</span><span class="hljs-number">12</span><span class="hljs-symbol">:mail</span><span class="hljs-symbol">:/var/spoooooool/mail</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">games:</span><span class="hljs-symbol">x:</span><span class="hljs-number">12</span><span class="hljs-symbol">:</span><span class="hljs-number">100</span><span class="hljs-symbol">:games</span><span class="hljs-symbol">:/usr/games</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">ftp:</span><span class="hljs-symbol">x:</span><span class="hljs-number">14</span><span class="hljs-symbol">:</span><span class="hljs-number">50</span><span class="hljs-symbol">:FTP</span> <span class="hljs-symbol">User:</span>/var/<span class="hljs-symbol">ftp:</span>/sbin/nologin
<span class="hljs-symbol">nobody:</span><span class="hljs-symbol">x:</span><span class="hljs-number">99</span><span class="hljs-symbol">:</span><span class="hljs-number">99</span><span class="hljs-symbol">:Nobody</span><span class="hljs-symbol">:/</span><span class="hljs-symbol">:/sbin/nologin</span>
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
systemd-<span class="hljs-symbol">network:</span><span class="hljs-symbol">x:</span><span class="hljs-number">192</span><span class="hljs-symbol">:</span><span class="hljs-number">192</span><span class="hljs-symbol">:systemd</span> Network <span class="hljs-symbol">Management:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">dbus:</span><span class="hljs-symbol">x:</span><span class="hljs-number">81</span><span class="hljs-symbol">:</span><span class="hljs-number">81</span><span class="hljs-symbol">:System</span> message <span class="hljs-symbol">bus:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">polkitd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">998</span><span class="hljs-symbol">:</span><span class="hljs-number">996</span><span class="hljs-symbol">:User</span> <span class="hljs-keyword">for</span> <span class="hljs-symbol">polkitd:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed '1,18'd test.txt</span>
<span class="hljs-symbol">polkitd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">998</span><span class="hljs-symbol">:</span><span class="hljs-number">996</span><span class="hljs-symbol">:User</span> <span class="hljs-keyword">for</span> <span class="hljs-symbol">polkitd:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>
ログイン後にコピー

注: 这种方法并不会删除文件的内容,知识打印在屏幕上的内容删除了指定行。那么如何直接删除文件中指定的内容呢?如下:

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># wc -l test.txt</span>
<span class="hljs-number">23</span> test.txt
[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -i '1,20'd test.txt</span>
[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># wc -l test.txt</span>
<span class="hljs-number">3</span> test.txt
[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># cat test.txt</span>
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>
[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed -i '/SSH/'d test.txt</span>
[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># cat test.txt</span>
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>
ログイン後にコピー

查找替换(s) 类似于vim中的查找替换

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed '1,10s/root/toor/g' test.txt</span>
<span class="hljs-symbol">toor:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:toor</span><span class="hljs-symbol">:/toor</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>
<span class="hljs-number">222222</span><span class="hljs-symbol">:r</span>.<span class="hljs-symbol">o:</span>dfdsfdsg
<span class="hljs-symbol">daemon:</span><span class="hljs-symbol">x:</span><span class="hljs-number">2</span><span class="hljs-symbol">:</span><span class="hljs-number">2</span><span class="hljs-symbol">:daemon</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">ad11om:</span><span class="hljs-symbol">x:</span><span class="hljs-number">3</span><span class="hljs-symbol">:</span><span class="hljs-number">4</span><span class="hljs-symbol">:ad1om</span><span class="hljs-symbol">:/var/adm</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">lp:</span><span class="hljs-symbol">x:</span><span class="hljs-number">4</span><span class="hljs-symbol">:</span><span class="hljs-number">7</span><span class="hljs-symbol">:lp</span><span class="hljs-symbol">:/var/spoool/lpd</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sync:</span><span class="hljs-symbol">x:</span><span class="hljs-number">5</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:sync</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/bin/sync</span>
<span class="hljs-symbol">shutdown:</span><span class="hljs-symbol">x:</span><span class="hljs-number">6</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:shutdown</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/shutdown</span>
<span class="hljs-symbol">halt:</span><span class="hljs-symbol">x:</span><span class="hljs-number">7</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:halo1t</span><span class="hljs-symbol">:/sbin</span><span class="hljs-symbol">:/sbin/halt</span>
<span class="hljs-symbol">mail:</span><span class="hljs-symbol">x:</span><span class="hljs-number">8</span><span class="hljs-symbol">:</span><span class="hljs-number">12</span><span class="hljs-symbol">:mail</span><span class="hljs-symbol">:/var/spoooooool/mail</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">operator:</span><span class="hljs-symbol">x:</span><span class="hljs-number">11</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:operator</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">games:</span><span class="hljs-symbol">x:</span><span class="hljs-number">12</span><span class="hljs-symbol">:</span><span class="hljs-number">100</span><span class="hljs-symbol">:games</span><span class="hljs-symbol">:/usr/games</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">ftp:</span><span class="hljs-symbol">x:</span><span class="hljs-number">14</span><span class="hljs-symbol">:</span><span class="hljs-number">50</span><span class="hljs-symbol">:FTP</span> <span class="hljs-symbol">User:</span>/var/<span class="hljs-symbol">ftp:</span>/sbin/nologin
<span class="hljs-symbol">nobody:</span><span class="hljs-symbol">x:</span><span class="hljs-number">99</span><span class="hljs-symbol">:</span><span class="hljs-number">99</span><span class="hljs-symbol">:Nobody</span><span class="hljs-symbol">:/</span><span class="hljs-symbol">:/sbin/nologin</span>
systemd-bus-<span class="hljs-symbol">proxy:</span><span class="hljs-symbol">x:</span><span class="hljs-number">999</span><span class="hljs-symbol">:</span><span class="hljs-number">997</span><span class="hljs-symbol">:systemd</span> Bus <span class="hljs-symbol">Proxy:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
systemd-<span class="hljs-symbol">network:</span><span class="hljs-symbol">x:</span><span class="hljs-number">192</span><span class="hljs-symbol">:</span><span class="hljs-number">192</span><span class="hljs-symbol">:systemd</span> Network <span class="hljs-symbol">Management:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">dbus:</span><span class="hljs-symbol">x:</span><span class="hljs-number">81</span><span class="hljs-symbol">:</span><span class="hljs-number">81</span><span class="hljs-symbol">:System</span> message <span class="hljs-symbol">bus:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">polkitd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">998</span><span class="hljs-symbol">:</span><span class="hljs-number">996</span><span class="hljs-symbol">:User</span> <span class="hljs-keyword">for</span> <span class="hljs-symbol">polkitd:</span>/<span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">tss:</span><span class="hljs-symbol">x:</span><span class="hljs-number">59</span><span class="hljs-symbol">:</span><span class="hljs-number">59</span><span class="hljs-symbol">:Account</span> used by the trousers package to sandbox the tcsd <span class="hljs-symbol">daemon:</span>/dev/<span class="hljs-symbol">null:</span>/sbin/nologin
<span class="hljs-symbol">postfix:</span><span class="hljs-symbol">x:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-number">89</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/spool/postfix</span><span class="hljs-symbol">:/sbin/nologin</span>
<span class="hljs-symbol">sshd:</span><span class="hljs-symbol">x:</span><span class="hljs-number">74</span><span class="hljs-symbol">:</span><span class="hljs-number">74</span><span class="hljs-symbol">:Privilege-separated</span> <span class="hljs-symbol">SSH:</span>/var/empty/<span class="hljs-symbol">sshd:</span>/sbin/nologin
<span class="hljs-symbol">chrony:</span><span class="hljs-symbol">x:</span><span class="hljs-number">997</span><span class="hljs-symbol">:</span><span class="hljs-number">995</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:/var/lib/chrony</span><span class="hljs-symbol">:/sbin/nologin</span>
此处如果不使用head命令,将会显示test.txt全部内容。

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># sed '1,10s/root/toor/g' test.txt | head -3</span>
<span class="hljs-symbol">toor:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:toor</span><span class="hljs-symbol">:/toor</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># cat test.txt | head -3</span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>
ログイン後にコピー

替换每行第一个字符串和最后一个字符串的位置:

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># head -3 test.txt</span>
<span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>
[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># head -3 test.txt |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'</span>
/bin/<span class="hljs-symbol">bash:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:root</span>
/sbin/<span class="hljs-symbol">NOLOGIN:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:bin</span>
<span class="hljs-number">325345345</span><span class="hljs-symbol">:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:dfsdfdsfsdfs</span>
ログイン後にコピー

说明: 小括号内的字符串可以用反斜杠跟数字组合按顺序来代替。

在每一行行首添加指定字符:

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># head -3 test.txt | sed -r 's/(.*)/AB:\1/'</span>
<span class="hljs-symbol">AB:</span><span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">AB:</span><span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">AB:</span><span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>

[root<span class="hljs-variable">@cham2</span> sed]<span class="hljs-comment"># head -3 test.txt | sed -r 's/(.*)/AB:&/'</span>
<span class="hljs-symbol">AB:</span><span class="hljs-symbol">root:</span><span class="hljs-symbol">x:</span><span class="hljs-number">0</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span><span class="hljs-symbol">:root</span><span class="hljs-symbol">:/root</span><span class="hljs-symbol">:/bin/bash</span>
<span class="hljs-symbol">AB:</span><span class="hljs-symbol">bin:</span><span class="hljs-symbol">x:</span><span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span><span class="hljs-symbol">:bin</span><span class="hljs-symbol">:/bin</span><span class="hljs-symbol">:/sbin/NOLOGIN</span>
<span class="hljs-symbol">AB:</span><span class="hljs-symbol">dfsdfdsfsdfs:</span><span class="hljs-number">1241124</span><span class="hljs-symbol">:jksdfhdskjf</span><span class="hljs-symbol">:sfdsf</span><span class="hljs-symbol">:rao</span><span class="hljs-symbol">:</span><span class="hljs-number">325345345</span>
ログイン後にコピー

说明: 在此&代表前面括号中的内容=“\ 1”

以上がsedコマンドツールの使い方の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:linuxprobe.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート