sed应用

sed基本用法

1.1 问题

本案例要求熟悉sed命令的p、d、s等常见操作,并结合正则表达式,完成以下任务:

  • 删除文件中每行的第二个、最后一个字符
  • 将文件中每行的第一个、第二个字符互换
  • 删除文件中所有的数字、行首的空格
  • 为文件中每个大写字母添加括号

1.2 方案

sed文本处理工具的用法:

  1. 用法1:前置命令 | sed [选项] ‘编辑指令‘
  2. 用法2:sed [选项] ‘编辑指令‘ 文件.. ..

相关说明如下:

  • “编辑指令”可以为增删改查等指令
  • “定址符”用来定义需要操作的文本,由“[地址1 [,地址2]]组成
  • 未指定“定址符”时,默认处理所有文本

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:认识sed工具的基本选项

1)sed命令的 -n 选项

执行p打印等过滤操作时,希望看到的是符合条件的文本。但不使用任何选项时,默认会将原始文本一并输出,从而干扰过滤效果。比如,尝试用sed输出/etc/rc.local的第1行:

  1. [[email protected] ~]# sed ‘1p‘ /etc/rc.local
  2. #!/bin/sh
  3. #!/bin/sh
  4. #
  5. #
  6. # This script will be executed *after* all the other init scripts.
  7. # You can put your own initialization stuff in here if you don‘t
  8. # want to do the full Sys V style init stuff.

可以发现所有的行都被显示出来了。—— 正确的用法应该添加 -n 选项,这样就可以只显示第1行了:

  1. [[email protected] ~]# sed -n ‘1p‘ /etc/rc.local
  2. #!/bin/s

而在执行d删除等过滤操作时,希望看到的是删除符合条件的文本之后还能够被保留下来的文本,所以这时候就不应该使用 -n 选项了。比如,删除/etc/rc.local文件的第1-4行文本:

  1. [[email protected] ~]# sed ‘1,4d‘ /etc/rc.local
  2. # want to do the full Sys V style init stuff.
  3. touch /var/lock/subsys/local

2)sed命令的 -i 选项

正常情况下,sed命令所做的处理只是把操作结果(包括打印、删除等)输出到当前终端屏幕,而并不会对原始文件做任何更改:

  1. [[email protected] ~]# sed ‘1,4d‘ rclocal.txt             //删除第1~4行,输出结果
  2. # want to do the full Sys V style init stuff.
  3. touch /var/lock/subsys/local
  4. [[email protected] ~]# cat rclocal.txt                 //查看原始文本,并未改动

若希望直接修改文件内容,应添加选项 -i 。

比如,直接删除rcloal.txt文件的第1~4行,不输出结果:

  1. [[email protected] ~]# sed -i ‘1,4d‘ rclocal.txt         //删除操作
  2. [[email protected] ~]# cat rclocal.txt                     //确认删除结果

下文中关于使用sed修改文件的示例中,为了避免大家在练习过程中因误操作导致系统故障,部分命令省略 –i 选项,不再逐一说明。需要时,大家可自行加上此选项。

3)多个指令可以使用分号隔离

用分号来隔离多个操作(如果有定址条件,则应该使用{ }括起来),比如:

  1. [[email protected] ~]# sed -n ‘1p;4p‘ /etc/rc.local
  2. #!/bin/sh
  3. # You can put your own initialization stuff in here if you don‘t

或者:

  1. [[email protected] ~]# sed -n ‘{1p;4p}‘ /etc/rc.local
  2. #!/bin/sh
  3. # You can put your own initialization stuff in here if you don‘t

步骤二:认识sed工具的p输出操作

先创建一个练习用的测试文件,每一行之前添加行号,方便练习时查看效果:

  1. [[email protected] ~]# cat -n /etc/rc.local > rclocal.txt
  2. [[email protected] ~]# cat rclocal.txt
  3. 1 #!/bin/sh
  4. 2 #
  5. 3 # This script will be executed *after* all the other init scripts.
  6. 4 # You can put your own initialization stuff in here if you don‘t
  7. 5 # want to do the full Sys V style init stuff.
  8. 6
  9. 7 touch /var/lock/subsys/local

1)输出所有行,相当于cat命令。

  1. [[email protected] ~]# sed -n ‘p‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script will be executed *after* all the other init scripts.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the full Sys V style init stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

2)输出第4行。

  1. [[email protected] ~]# sed -n ‘4p‘ rclocal.txt
  2. 4 # You can put your own initialization stuff in here if you don‘t

3)输出第4~7行。

  1. [[email protected] ~]# sed -n ‘4,7p‘ rclocal.txt
  2. 4 # You can put your own initialization stuff in here if you don‘t
  3. 5 # want to do the full Sys V style init stuff.
  4. 6
  5. 7 touch /var/lock/subsys/local

4)输出第4行和第7行。

  1. [[email protected] ~]# sed -n ‘4p;7p‘ rclocal.txt
  2. 4 # You can put your own initialization stuff in here if you don‘t
  3. 7 touch /var/lock/subsys/local

5)输出第2行及之后的3行。

  1. [[email protected] ~]# sed -n ‘2,+3p‘ rclocal.txt
  2. 2 #
  3. 3 # This script will be executed *after* all the other init scripts.
  4. 4 # You can put your own initialization stuff in here if you don‘t
  5. 5 # want to do the full Sys V style init stuff.

6)输出以local结尾的行。

  1. [[email protected] ~]# sed -n ‘/local$/p‘ rclocal.txt
  2. 7 touch /var/lock/subsys/local

7)输出奇数行。

  1. [[email protected] ~]# sed -n ‘p;n‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 3 # This script will be executed *after* all the other init scripts.
  4. 5 # want to do the full Sys V style init stuff.
  5. 7 touch /var/lock/subsys/local

8)输出偶数行。

  1. [[email protected] ~]# sed -n ‘n;p‘ rclocal.txt
  2. 2 #
  3. 4 # You can put your own initialization stuff in here if you don‘t
  4. 6

9)从第5行输出到最后一行。

  1. [[email protected] ~]# sed -n ‘5,$p‘ rclocal.txt
  2. 5 # want to do the full Sys V style init stuff.
  3. 6
  4. 7 touch /var/lock/subsys/local

10)输出文本的行数。

  1. [[email protected] ~]# sed -n ‘$=‘ rclocal.txt
  2. 7

步骤三:认识sed工具的d输出操作

还以rclocal.txt文件为例,文件内容如下所示:

  1. [[email protected] ~]# cat rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script will be executed *after* all the other init scripts.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the full Sys V style init stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

1)删除第3~5行文本

  1. [[email protected] ~]# sed ‘3,5d‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 6
  5. 7 touch /var/lock/subsys/local

2)删除所有包含“init”的行

  1. [[email protected] ~]# sed ‘/init/d‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 6
  5. 7 touch /var/lock/subsys/local

3)删除所有包含“init”的行、所有包含“bin”的行

  1. [[email protected] ~]# sed ‘/init/d;/bin/d‘ rclocal.txt
  2. 2 #
  3. 6
  4. 7 touch /var/lock/subsys/local

4)删除不包括“init”的行

  1. [[email protected] ~]# sed ‘/init/!d‘ rclocal.txt
  2. 3 # This script will be executed *after* all the other init scripts.
  3. 4 # You can put your own initialization stuff in here if you don‘t
  4. 5 # want to do the full Sys V style init stuff.

这个实际效果相当于只显示包含“init”的行:

  1. [[email protected] ~]# sed -n ‘/init/p‘ rclocal.txt
  2. 3 # This script will be executed *after* all the other init scripts.
  3. 4 # You can put your own initialization stuff in here if you don‘t
  4. 5 # want to do the full Sys V style init stuff.

5)删除文件的最后一行

  1. [[email protected] ~]# sed ‘$d‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script will be executed *after* all the other init scripts.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the full Sys V style init stuff.
  7. 6

6)删除文件中的空行

手动新建一个测试文件:

  1. [[email protected] ~]# vim blankline.txt
  2. abc
  3. def
  4. hijklmn
  5. hello world
  6. I am here
  7. end

删除所有空行:

  1. [[email protected] ~]# sed ‘/^$/d‘ blankline.txt
  2. abc
  3. def
  4. hijklmn
  5. hello world
  6. I am here
  7. end

步骤四:认识sed工具的s替换操作

还以rclocal.txt文件为例,文件内容如下所示:

  1. [[email protected] ~]# cat rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script will be executed *after* all the other init scripts.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the full Sys V style init stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

1)将所有行中的第一个“ll”(如果有的话)替换为“TARENA”。

  1. [[email protected] ~]# sed ‘s/ll/TARENA/‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script wiTARENA be executed *after* all the other init scripts.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the fuTARENA Sys V style init stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

2)将所有的“ll”(如果有的话)替换为“TARENA”。

  1. [[email protected] ~]# sed ‘s/ll/TARENA/g‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script wiTARENA be executed *after* aTARENA the other init scripts.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the fuTARENA Sys V style init stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

3)将第3行内的第2个“script”替换为“SCRIPT”。

  1. [[email protected] ~]# sed ‘3s/script/SCRIPT/2‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script will be executed *after* all the other init SCRIPTs.
  5. 4 # You can put your own initialization stuff in here if you don‘t
  6. 5 # want to do the full Sys V style init stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

4)删除文件内指定的字符串(替换为空)。

删除所有的“init”字符串:

  1. [[email protected] ~]# sed ‘s/init//g‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This script will be executed *after* all the other scripts.
  5. 4 # You can put your own ialization stuff in here if you don‘t
  6. 5 # want to do the full Sys V style stuff.
  7. 6
  8. 7 touch /var/lock/subsys/local

删除所有的“script”、所有的“stuff”、所有的字母e,或者的关系用转义方式 \| 来表示:

  1. [[email protected] ~]# sed ‘s/script\|stuff\|e//g‘ rclocal.txt
  2. 1 #!/bin/sh
  3. 2 #
  4. 3 # This will b xcutd *aftr* all th othr init s.
  5. 4 # You can put your own initialization in hr if you don‘t
  6. 5 # want to do th full Sys V styl init .
  7. 6
  8. 7 touch /var/lock/subsys/local

5)配置行的注释、解除注释。

以真实文件/etc/rc.local为例,文件内容如下:

  1. [[email protected] ~]# cat /etc/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don‘t
  6. # want to do the full Sys V style init stuff.
  7. touch /var/lock/subsys/local

解除/etc/rc.local文件第3~5行的注释(去掉开头的 # ):

  1. [[email protected] ~]# sed ‘3,5s/^#//‘ /etc/rc.local
  2. #!/bin/sh
  3. #
  4. This script will be executed *after* all the other init scripts.
  5. You can put your own initialization stuff in here if you don‘t
  6. want to do the full Sys V style init stuff.
  7. touch /var/lock/subsys/local

将/etc/rc.local文件的第6~7行注释掉(行首添加 # ):

  1. [[email protected] ~]# sed ‘6,7s/^/#/‘ /etc/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don‘t
  6. # want to do the full Sys V style init stuff.
  7. #
  8. #touch /var/lock/subsys/local

步骤五:利用sed完成本例要求的任务

参考数据文件内容如下:

  1. [[email protected] ~]# cat nssw.txt
  2. An example Name Service Switch config file. This file should be
  3. sorted with the most-used services at the beginning.
  4. #
  5. The entry ‘[NOTFOUND=return]‘ means that the search for an
  6. entry should stop if the search in the previous entry turned
  7. up nothing. Note that if the search failed due to some other reason
  8. (like no NIS server responding) then the search continues with the

本小节的操作使用nssw.txt作为测试文件。

1)删除文件中每行的第二个、最后一个字符

分两次替换操作,第一次替换掉第2个字符,第二次替换掉最后一个字符:

  1. [[email protected] ~]# sed ‘s/.//2;s/.$//‘ nssw.txt
  2. A example Name Service Switch config file. This file should b
  3. srted with the most-used services at the beginning
  4. #
  5. Te entry ‘[NOTFOUND=return]‘ means that the search for a
  6. etry should stop if the search in the previous entry turne
  7. u nothing. Note that if the search failed due to some other reaso
  8. (ike no NIS server responding) then the search continues with th

2)将文件中每行的第一个、第二个字符互换

每行文本拆分为“第1个字符”、“第2个字符”、“剩下的所有字符”三个部分,然后通过替换操作重排顺序为“2-1-3”:

  1. [[email protected] ~]# sed -r ‘s/^(.)(.)(.*)/\2\1\3/‘ nssw.txt
  2. nA example Name Service Switch config file. This file should be
  3. osrted with the most-used services at the beginning.
  4. #
  5. hTe entry ‘[NOTFOUND=return]‘ means that the search for an
  6. netry should stop if the search in the previous entry turned
  7. pu nothing. Note that if the search failed due to some other reason
  8. l(ike n up . Note that if the search failed due to some other
  9. (like NIS server responding) then the search continues with

3)删除文件中所有的数字、行首的空格

因原文件内没有数字,行首也没有空格,这里稍作做一点处理,生成一个新测试文件:

  1. [[email protected] ~]# sed ‘s/o/o7/;s/l/l4/;3,5s/^/ /‘ nssw.txt > nssw2.txt
  2. [[email protected] ~]# cat nssw2.txt
  3. An exampl4e Name Service Switch co7nfig file. This file should be
  4. so7rted with the most-used services at the beginning.
  5. #
  6. The entry ‘[NOTFOUND=return]‘ means that the search fo7r an
  7. entry sho7ul4d stop if the search in the previous entry turned
  8. up no7thing. Note that if the search fail4ed due to some other reason
  9. (l4ike no7 NIS server responding) then the search continues with the

以nssw2.txt文件为例,删除所有数字、行首空格的操作如下:

  1. [[email protected] ~]# sed -r ‘s/[0-9]//g;s/^( )+//‘ nssw2.txt

4)为文件中每个大写字母添加括号

使用“&”可调用s替换操作中的整个查找串,所以可参考下列操作解决:

  1. [[email protected] ~]# sed ‘s/[A-Z]/(&)/g‘ nssw.txt
  2. (A)n example (N)ame (S)ervice (S)witch config file. (T)his file should be
  3. sorted with the most-used services at the beginning.
  4. #
  5. (T)he entry ‘[(N)(O)(T)(F)(O)(U)(N)(D)=return]‘ means that the search for an
  6. entry should stop if the search in the previous entry turned
  7. up nothing. (N)ote that if the search failed due to some other reason
  8. (like no (N)(I)(S) server responding) then the search continues with the

2 案例2:使用sed修改系统配置

2.1 问题

本案例要求熟悉课上的sed应用案例,并编写脚本anonftp.sh,实现以下功能:

  • 通过yum安装vsftpd软件包
  • 修改vsftpd服务配置,开启匿名上传
  • 调整/var/ftp/pub目录权限,允许ftp写入
  • 启动vsftpd服务,并设置开机自运行

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:认识课堂上的sed练习

1)修改默认运行级别

将默认运行级别修改为5,确认修改结果:

  1. [[email protected] ~]# sed -i ‘/^id:/s/3/5/‘ /etc/inittab
  2. [[email protected] ~]# grep "^id:" /etc/inittab
  3. id:5:initdefault:

再改回去:

  1. [[email protected] ~]# sed -i ‘/^id:/s/5/3/‘ /etc/inittab
  2. [[email protected] ~]# grep "^id:" /etc/inittab
  3. id:3:initdefault:

2)修改IP地址的网段部分,主机地址不变。

直接修改网卡eth0的配置文件,检查原有的配置内容:

  1. [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  2. DEVICE=eth0
  3. BOOTPROTO=none
  4. HWADDR=00:0c:29:82:09:e9
  5. ONBOOT=yes
  6. NETMASK=255.255.255.0
  7. IPADDR=192.168.4.4
  8. TYPE=Ethernet

若希望将IP地址192.168.4.4修改为172.16.16.4,则应该定位到“IPADDR”所在的行,执行相应的替换(仅测试,尚未修改):

  1. [[email protected] ~]# sed ‘/^IPADDR/s/192.168.4.4/172.16.16.4/‘ \
  2. /etc/sysconfig/network-scripts/ifcfg-eth0 | grep "^IPADDR"
  3. IPADDR=172.16.16.4

要求只修改网段地址时,可以利用扩展正则表达式的 \1、\2、……等调用,分别对应此前第1个、第2个、…… 以 ()包围的表达式所匹配的内容。

所以上述操作可以改为如下(启用扩展匹配应添加 -r 选项):

  1. [[email protected] ~]# sed -r -i ‘/^IPADDR/s/192.168.4.(.*)/172.16.16.\1/‘ \
  2. /etc/sysconfig/network-scripts/ifcfg-eth0

确认修改结果:

  1. [[email protected] ~]# grep "^IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0
  2. IPADDR=172.16.16.4

再改回去:

  1. [[email protected] ~]# sed -r -i ‘/^IPADDR/s/172.16.16.(.*)/192.168.4.\1/‘ \
  2. /etc/sysconfig/network-scripts/ifcfg-eth0
  3. [[email protected] ~]# grep "^IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0
  4. IPADDR=192.168.4.4

3)调整httpd服务配置,更改网站根目录

由于需要替换的字符串中有 / ,为了避免与sed替换操作的分隔混淆,可以使用其他字符作为替换分隔,比如可改用“s#old#new#”的方式实现替换:

  1. [[email protected] ~]# sed -i ‘s#/var/www/html#/opt/wwwroot#‘ \
  2. /etc/httpd/conf/httpd.conf
  3. [[email protected] ~]# grep "^DocumentRoot" /etc/httpd/conf/httpd.conf
  4. DocumentRoot "/opt/wwwroot"

若要恢复,可再改回去:

  1. [[email protected] ~]# sed -i ‘s#/opt/wwwroot#/var/www/html#‘\
  2. /etc/httpd/conf/httpd.conf
  3. [[email protected] ~]# grep "^DocumentRoot" /etc/httpd/conf/httpd.conf
  4. DocumentRoot "/var/www/html"

步骤二:编写anonftp.sh脚本,用来装配匿名FTP服务

1)任务需求及思路分析

vsftpd服务的安装、改目录权限、起服务等操作可以直接写在脚本中。

修改vsftpd.conf配置的工作可以使用sed命令,根据默认配置,只需要定位到以#anon开头的行,去掉开头的注释即可。

2)根据实现思路编写脚本文件

  1. [[email protected] ~]# vim anonftp.sh
  2. #!/bin/bash
  3. yum -y install vsftpd                             //安装vsftpd软件
  4. cp /etc/vsftpd/vsftpd.conf{,.bak}                 //备份默认的配置文件
  5. sed -i "/^#anon/s/^#//" /etc/vsftpd/vsftpd.conf     //修改服务配置
  6. chown ftp /var/ftp/pub                             //调整目录权限
  7. /etc/init.d/vsftpd restart                         //启动服务
  8. chkconfig vsftpd on                                 //设为自动运行
  9. [[email protected] ~]# chmod +x anonftp.sh

3)验证、测试脚本

运行脚本anonftp.sh:

  1. [[email protected] ~]# ./anonftp.sh
  2. .. ..
  3. Installed:
  4. vsftpd.x86_64 0:2.0.5-28.el5
  5. Complete!
  6. 关闭 vsftpd: [失败]
  7. 为 vsftpd 启动 vsftpd: [确定]

使用ftp登录服务,测试是否可以上传:

  1. [[email protected] ~]# ftp localhost                         //本机访问测试
  2. Connected to localhost.localdomain.
  3. 220 (vsFTPd 2.0.5)
  4. 530 Please login with USER and PASS.
  5. 530 Please login with USER and PASS.
  6. KERBEROS_V4 rejected as an authentication type
  7. Name (localhost:root): ftp                         //匿名登录
  8. 331 Please specify the password.
  9. Password:
  10. 230 Login successful.
  11. Remote system type is UNIX.
  12. Using binary mode to transfer files.
  13. ftp> cd pub                                         //切换到 pub/ 目录
  14. 250 Directory successfully changed.
  15. ftp> put install.log                         //上传当前目录下的install.log 文件
  16. local: install.log remote: install.log
  17. 227 Entering Passive Mode (127,0,0,1,192,127)
  18. 150 Ok to send data.
  19. 226 File receive OK.
  20. 33139 bytes sent in 0.0065 seconds (5e+03 Kbytes/s)
  21. ftp> quit                                         //断开FTP连接
  22. 221 Goodbye.

查看/var/ftp/pub新上传的文件:

  1. [[email protected] ~]# ls -lh /var/ftp/pub/
  2. 总计 36K
  3. -rw------- 1 ftp ftp 33K 12-13 18:25 install.log

3 案例3:sed多行文本处理

3.1 问题

本案例要求使用sed工具来完成下列任务操作:

  • 修改主机名配置文件
  • 修改hosts文件,添加两条映射记录:192.168.4.5 与 svr5.tarena.com、svr5,还有119.75.217.56与www.baidu.com

3.2 方案

sed工具的多行文本处理操作:

  • i:在指定的行之前插入文本
  • a:在指定的行之后追加文本
  • c:替换指定的行

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改主机名配置文件

1)确认修改前的配置

  1. [[email protected] ~]# cat /etc/sysconfig/network
  2. NETWORKING=yes
  3. HOSTNAME=svr5.tarena.com

2)使用sed修改主机名配置所在行的内容(c整行替换)

  1. [[email protected] ~]# sed ‘/^HOSTNAME/cHOSTNAME=mysvr.tarena.com‘ /etc/sysconfig/network

步骤二:修改hosts文件,添加新的记录

1)确认修改前的配置

  1. [[email protected] ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

2)使用sed修改hosts文件,添加两行新纪录(a追加)

  1. [[email protected] ~]# sed -i ‘$a192.168.4.5 svr5.tarena.com svr5\
  2. > 119.75.217.56 www.baidu.com‘ /etc/hosts
  3. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  4. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  5. 192.168.4.5 svr5.tarena.com svr5
  6. 119.75.217.56 www.baidu.com

4 案例4:sed综合脚本应用

4.1 问题

本案例要求编写脚本getupwd.sh,实现以下需求:

  • 找到使用bash作登录Shell的本地用户
  • 列出这些用户的shadow密码记录
  • 按每行“用户名 --> 密码记录”保存到getupwd.log,如图-1所示

图-1

4.2 方案

基本思路如下:

  1. 先用sed工具取出登录Shell为/bin/bash的用户记录,保存为临时文件/tmp/urec.tmp,并计算记录数量
  2. 再结合while循环遍历取得的账号记录,逐行进行处理
  3. 针对每一行用户记录,采用掐头去尾的方式获得用户名、密码字串
  4. 按照指定格式追加到/tmp/getuupwd.log文件
  5. 结束循环后删除临时文件,报告分析结果

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:编写getupwd.sh脚本

  1. [[email protected] ~]# vim ./getupwd.sh
  2. #/bin/bash
  3. > /tmp/getupwd.log                 ## 创建空文件
  4. sed -n ‘/:\/bin\/bash$/w /tmp/urec.tmp‘ /etc/passwd     ## 提取符合条件的账号记录
  5. UNUM=$(egrep -c ‘.‘ /tmp/urec.tmp)                 ## 取得记录个数
  6. while [ ${i:=1} -le $UNUM ]                     ## 从第1行开始,遍历账号记录
  7. do
  8. UREC=$(sed -n "${i}p" /tmp/urec.tmp)             ## 取指定行数的记录
  9. NAME=${UREC%%:*}                         ## 截取用户名(记录去尾)
  10. PREC=$(sed -n "/^$NAME:/p" /etc/shadow)         ## 查找与用户名对应的密码记录
  11. PASS=${PREC#*:}                         ## 掐头
  12. PASS=${PASS%%:*}                     ## 去尾,只留下密码记录
  13. echo "$NAME --> $PASS" >> /tmp/getupwd.log         ## 保存结果
  14. let i++                         ## 自增1,转下一次循环
  15. done
  16. /bin/rm -rf /tmp/urec.tmp                 ## 删除临时文件
  17. echo "用户分析完毕,请查阅文件 /tmp/getupwd.log"         ## 完成后提示
  18. [[email protected] ~]# chmod +x ./getupwd.sh

步骤二:测试、验证执行结果

  1. [[email protected] ~]# ./getupwd.sh
  2. 用户分析完毕,请查阅文件 /tmp/getupwd.log
  3. [[email protected] ~]# less /tmp/getupwd.log
  4. root --> $6$IWgMYmRACwdbfwBo$dr8Yn983nswiJVw0dTMjzbDvSLeCd1GMYjbvsDiFEkL8jnXOLcocBQypOCr4C6BRxNowIxjh6U2qeFU0u1LST/
  5. zengye --> $6$Qb37LOdzRl5995PI$L0zTOgnhGz8ihWkW81J.5XhPp/l7x2./Me2ag0S8tRndCBL9nIjHIKkUKulHxJ6TXyHYmffbVgUT6pbSwf8O71
  6. clamav --> !!
  7. mysql --> !!
  8. abc --> !!
  9. .. ..

原文地址:https://www.cnblogs.com/xiaoren112/p/8270099.html

时间: 2024-08-29 05:42:43

sed应用的相关文章

Linux学习笔记之grep命令及sed 命令相关选项

#grep  强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 根据模式,搜索文本 ,并将符合模式的文本行显示出来,常与正则表达式相结合使用. [[email protected] ~]# grep --h 用法: grep [选项]... PATTERN [FILE]... 在每个 FILE 或是标准输入中查找 PATTERN. 默认的 PATTERN 是一个基本正则表达式(缩写为 BRE). 例如: grep -i 'hello world' menu.h main.c

shell sed 的一些用法

#1.替换 #将1.txt文件中的2017替换成2016显示在屏幕上 sed "s/2017/2016/g" 1.txt #将1.txt文件中的2017替换成2016显示在屏幕上对文件彻底修改,加 i 修改进文件 sed -i "s/2017/2016/g" 1.txt #Sed读取系统变量,变量替换 DATE="2016" sed  "s/2017/$DATE/g" 1.txt #关闭SELinux,修改SELinux策略e

使用sed命令匹配文件指定的行删除

[email protected]123-57-87-170 ~ # cat file #aaabbbcccddd #aaabbbcccddd #aaabbbcccddd #cccdddaaawwee #cccdddaaawwee #cccdddaaawwee #cccdddaaawwee [email protected]-123-57-87-170 ~ # sed -i '/aaabbbcccddd/ s/#//g' file [email protected]-123-57-87-170

sed 实践案例 (不定期更新)

删除每行第一个字符 sed -r 's/^.//g' /etc/passwd 删掉每行第二个字符 sed -r 's/^(.)(.)/\1/g' /etc/passwd 删掉每行最后一个字符 sed -r 's/.$//g' /etc/passwd 取消一行的注释 cat cas.properties | sed '/Oracle10gDialect$/ s/^#//g' 给某行增加注释 cat cas.properties | sed -r '/MySQLDialect$/ s/^(.)/#\

Linux学习笔记:sed

sed命令:vim的末行命令(ex模式命令): sed:Stream EDitor,流编辑器,以行为单位对一个或多个文件进行编辑处理:每一次sed都会处理给定文件中的一行内容. 在sed处理文本时,将正在处理的当前行存储到临时的缓冲区中,称为"模式空间":用当前行去匹配给定的PATTERN,如果能匹配,则使用command编辑处理:如果不匹配,则默认输出至标准输出:然后继续处理下一行,直到文件的末尾. 默认情况下,sed的所有处理行为不会影响源文件的内容: 我们一般会使用sed命令来自

sed入门详解教程

sed 是一个比较古老的,功能十分强大的用于文本处理的流编辑器,加上正则表达式的支持,可以进行大量的复杂的文本编辑操作.sed 本身是一个非常复杂的工具,有专门的书籍讲解 sed 的具体用法,但是个人觉得没有必要去学习它的每个细节,那样没有特别大的实际意义.网上也有很多关于 sed 的教程,我也是抱着学习的心态来学习 sed 的常见的用法,并进行系统的总结,内容基本覆盖了 sed 的大部分的知识点.文中的内容比较简练,加以实际示例来帮助去理解 sed 的使用. 一.写在前边 1.sed介绍 se

Linux高级文本处理之sed(四)

模式空间是sed内部维护的一个缓存空间,它存放着读入的一行或者多行内容.但是模式空间的一个限制是无法保存模式空间中被处理的行,因此sed又引入了另外一个缓存空间--模式空间(Hold Space). 一.保持空间 保持空间用于保存模式空间的内容,模式空间的内容可以复制到保持空间,同样地保持空间的内容可以复制回模式空间.sed提供了几组命令用来完成复制的工作,其它命令无法匹配也不能修改模式空间的内容. 操作保持空间的命令如下所示: 这几组命令提供了保存.取回以及交换三个动作,交换命令比较容易理解,

sed命令使用详解归纳

用法 sed [option] 'Address Command' yourfile e.g. sed -n '2,13p' lineuser #打印文件lineuser中第2~13行的内容,-n为option, 2,13为Address,p为Command Address与Command中间的空格也可以省略 sed处理过程 待处理的文件一行一行地被sed读入到模式空间(pattern space)中,根据匹配及相应的处理命令进行输出显示,默认下每个模式空间的内容都会输出 sed常用option

sed: -i requires an argument Permission denied

在脚本中使用sed时,如果一行的内容较长,为了脚本美观要换行,在行尾添加"\"即可. 注意:如果在"\"之后添加空格,例如: sed -i "/^xxx=/s/.*/xxx=$xxx/"\[:space:]        $xxx echo $? [:space:]表示行尾有一个空格 脚本会报如下错误: sed: -i requires an argument./xxx.sh: line 20: xxx.sh: Permission denied

Linux 的文本处理工具---sed

                sed 处理文本工具 一.概述 Stream EDitor,行编辑器 sed是一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕.接着处理下一行,这样不断重复,直到文件末尾.文件内容并没有改变,除非你使用重定向存储输出.sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等  sed 是一种非