首页 > 系统教程 > LINUX > 正文

sed命令工具使用方法

王林
发布: 2024-04-13 13:40:01
转载
872人浏览过

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)

Git版本控制与工作流 中文WORD版
Git版本控制与工作流 中文WORD版

篇文章是针对git版本控制和工作流的总结,如果有些朋友之前还没使用过git,对git的基本概念和命令不是很熟悉,可以从以下基本教程入手: Git是分布式版本控制系统,与SVN类似的集中化版本控制系统相比,集中化版本控制系统虽然能够令多个团队成员一起协作开发,但有时如果中央服务器宕机的话,谁也无法在宕机期间提交更新和协同开发。甚至有时,中央服务器磁盘故障,恰巧又没有做备份或备份没及时,那就可能有丢失数据的风险。感兴趣的朋友可以过来看看

Git版本控制与工作流 中文WORD版 0
查看详情 Git版本控制与工作流 中文WORD版
[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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:Linux就该这么学网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号