码迷,mamicode.com
首页 > 系统相关 > 详细

linux sed用法

时间:2016-07-12 15:40:38      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:linux sed

linux   sed用法

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

1、以行为单位的新增/删除

(1)将 /etc/passwd 的内容列出及第 2~5 行删除,然后保存在passwd_sed.txt

cp /etc/passwd /root/shell/
[root@s80sit shell]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
userlog:x:816:816::/home/userlog:/bin/bash
tomcat:x:815:815::/home/tomcat:/bin/bash
manager:x:817:817::/home/manager:/bin/bash
cloudera-scm:x:496:492:Cloudera Manager:/var/lib/cloudera-scm-server:/sbin/nologin
flume:x:495:490:Flume:/var/lib/flume-ng:/bin/false
hdfs:x:494:489:Hadoop HDFS:/var/lib/hadoop-hdfs:/bin/bash
solr:x:493:488:Solr:/var/lib/solr:/sbin/nologin
zookeeper:x:492:487:ZooKeeper:/var/lib/zookeeper:/bin/false
llama:x:491:486:Llama:/var/lib/llama:/bin/bash
httpfs:x:490:485:Hadoop HTTPFS:/var/lib/hadoop-httpfs:/bin/bash
mapred:x:489:484:Hadoop MapReduce:/var/lib/hadoop-mapreduce:/bin/bash
sqoop:x:488:483:Sqoop:/var/lib/sqoop:/bin/false
yarn:x:487:482:Hadoop Yarn:/var/lib/hadoop-yarn:/bin/bash
kms:x:486:481:Hadoop KMS:/var/lib/hadoop-kms:/bin/bash
hive:x:485:480:Hive:/var/lib/hive:/bin/false
sqoop2:x:484:479:Sqoop 2 User:/var/lib/sqoop2:/sbin/nologin
oozie:x:483:478:Oozie User:/var/lib/oozie:/bin/false
hbase:x:482:477:HBase:/var/lib/hbase:/bin/false
sentry:x:481:476:Sentry:/var/lib/sentry:/sbin/nologin
impala:x:480:475:Impala:/var/lib/impala:/bin/bash
spark:x:479:474:Spark:/var/lib/spark:/sbin/nologin
hue:x:478:473:Hue:/usr/lib/hue:/bin/false
oracle:x:818:819::/home/oracle:/bin/bash
infa:x:819:821::/home/infa:/bin/bash
zabbix:x:477:472:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
cat passwd | sed ‘2,5d‘ > passwd_sed.txt
[root@s80sit shell]# cat passwd_sed.txt 
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
userlog:x:816:816::/home/userlog:/bin/bash
tomcat:x:815:815::/home/tomcat:/bin/bash
manager:x:817:817::/home/manager:/bin/bash
cloudera-scm:x:496:492:Cloudera Manager:/var/lib/cloudera-scm-server:/sbin/nologin
flume:x:495:490:Flume:/var/lib/flume-ng:/bin/false
hdfs:x:494:489:Hadoop HDFS:/var/lib/hadoop-hdfs:/bin/bash
solr:x:493:488:Solr:/var/lib/solr:/sbin/nologin
zookeeper:x:492:487:ZooKeeper:/var/lib/zookeeper:/bin/false
llama:x:491:486:Llama:/var/lib/llama:/bin/bash
httpfs:x:490:485:Hadoop HTTPFS:/var/lib/hadoop-httpfs:/bin/bash
mapred:x:489:484:Hadoop MapReduce:/var/lib/hadoop-mapreduce:/bin/bash
sqoop:x:488:483:Sqoop:/var/lib/sqoop:/bin/false
yarn:x:487:482:Hadoop Yarn:/var/lib/hadoop-yarn:/bin/bash
kms:x:486:481:Hadoop KMS:/var/lib/hadoop-kms:/bin/bash
hive:x:485:480:Hive:/var/lib/hive:/bin/false
sqoop2:x:484:479:Sqoop 2 User:/var/lib/sqoop2:/sbin/nologin
oozie:x:483:478:Oozie User:/var/lib/oozie:/bin/false
hbase:x:482:477:HBase:/var/lib/hbase:/bin/false
sentry:x:481:476:Sentry:/var/lib/sentry:/sbin/nologin
impala:x:480:475:Impala:/var/lib/impala:/bin/bash
spark:x:479:474:Spark:/var/lib/spark:/sbin/nologin
hue:x:478:473:Hue:/usr/lib/hue:/bin/false
oracle:x:818:819::/home/oracle:/bin/bash
infa:x:819:821::/home/infa:/bin/bash
zabbix:x:477:472:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin

(2)、只删除第二行

[root@s80sit shell]# cat passwd | sed ‘2d‘
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
userlog:x:816:816::/home/userlog:/bin/bash
tomcat:x:815:815::/home/tomcat:/bin/bash
manager:x:817:817::/home/manager:/bin/bash
cloudera-scm:x:496:492:Cloudera Manager:/var/lib/cloudera-scm-server:/sbin/nologin
flume:x:495:490:Flume:/var/lib/flume-ng:/bin/false
hdfs:x:494:489:Hadoop HDFS:/var/lib/hadoop-hdfs:/bin/bash
solr:x:493:488:Solr:/var/lib/solr:/sbin/nologin
zookeeper:x:492:487:ZooKeeper:/var/lib/zookeeper:/bin/false
llama:x:491:486:Llama:/var/lib/llama:/bin/bash
httpfs:x:490:485:Hadoop HTTPFS:/var/lib/hadoop-httpfs:/bin/bash
mapred:x:489:484:Hadoop MapReduce:/var/lib/hadoop-mapreduce:/bin/bash
sqoop:x:488:483:Sqoop:/var/lib/sqoop:/bin/false
yarn:x:487:482:Hadoop Yarn:/var/lib/hadoop-yarn:/bin/bash
kms:x:486:481:Hadoop KMS:/var/lib/hadoop-kms:/bin/bash
hive:x:485:480:Hive:/var/lib/hive:/bin/false
sqoop2:x:484:479:Sqoop 2 User:/var/lib/sqoop2:/sbin/nologin
oozie:x:483:478:Oozie User:/var/lib/oozie:/bin/false
hbase:x:482:477:HBase:/var/lib/hbase:/bin/false
sentry:x:481:476:Sentry:/var/lib/sentry:/sbin/nologin
impala:x:480:475:Impala:/var/lib/impala:/bin/bash
spark:x:479:474:Spark:/var/lib/spark:/sbin/nologin
hue:x:478:473:Hue:/usr/lib/hue:/bin/false
oracle:x:818:819::/home/oracle:/bin/bash
infa:x:819:821::/home/infa:/bin/bash
zabbix:x:477:472:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin

(3)、只删除第3行到最后  sed ‘3,$d‘

[root@s80sit shell]# cat passwd | sed ‘3,$d‘
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

(4)、第二行后(亦即是加在第三行)加上『I am a good boy』字样!只要用 2i就可以了

cat  passwd | sed ‘2i I am a good boy‘
[root@s80sit shell]# head -5  passwd | sed ‘2i I am a good boy‘ 
root:x:0:0:root:/root:/bin/bash
I am a good boy
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

(5)要增加两行以上
比如第二行、第三行增加分别aaaa bbbb
每一行之间都必须要以反斜杠[\]来进行新行的添加喔!

[root@s80sit shell]# head -10 passwd | sed ‘2a  aaaaa> bbbb ‘
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
aaaaa
bbbb 
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

2、以行为单位的替换与显示
(1)、将第2-5行的内容取代成为[aaaa]呢?
head -8  /etc/passwd | sed ‘2,5c aaaa‘

[root@s80sit shell]# head -8  /etc/passwd | sed ‘2,5c aaaa‘
root:x:0:0:root:/root:/bin/bash
aaaa
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@s80sit shell]# head -8  /etc/passwd | sed ‘2,4c aaaa‘
root:x:0:0:root:/root:/bin/bash
aaaa
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

通过这个方法我们就能够将数据整行取代了哦!!!!!!!!

(2)列出 /etc/passwd 文件内的第 5-7 行
head -10 passwd | sed -n ‘5,7p‘

[root@s80sit shell]# head -10 passwd | sed -n ‘5,7p‘
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

结论: sed 的以行为单位的显示功能,就能够将某一个文件内的某些行号选择出来显示。

3、数据的搜寻并显示
搜索 /etc/passwd有root关键字的行
(1)没有参数  (这里除了输出所有行,还输出匹配的行)

[root@s80sit shell]# cat passwd | sed   ‘/root/p‘
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
userlog:x:816:816::/home/userlog:/bin/bash
tomcat:x:815:815::/home/tomcat:/bin/bash
manager:x:817:817::/home/manager:/bin/bash
cloudera-scm:x:496:492:Cloudera Manager:/var/lib/cloudera-scm-server:/sbin/nologin
flume:x:495:490:Flume:/var/lib/flume-ng:/bin/false
hdfs:x:494:489:Hadoop HDFS:/var/lib/hadoop-hdfs:/bin/bash
solr:x:493:488:Solr:/var/lib/solr:/sbin/nologin
zookeeper:x:492:487:ZooKeeper:/var/lib/zookeeper:/bin/false
llama:x:491:486:Llama:/var/lib/llama:/bin/bash
httpfs:x:490:485:Hadoop HTTPFS:/var/lib/hadoop-httpfs:/bin/bash
mapred:x:489:484:Hadoop MapReduce:/var/lib/hadoop-mapreduce:/bin/bash
sqoop:x:488:483:Sqoop:/var/lib/sqoop:/bin/false
yarn:x:487:482:Hadoop Yarn:/var/lib/hadoop-yarn:/bin/bash
kms:x:486:481:Hadoop KMS:/var/lib/hadoop-kms:/bin/bash
hive:x:485:480:Hive:/var/lib/hive:/bin/false
sqoop2:x:484:479:Sqoop 2 User:/var/lib/sqoop2:/sbin/nologin
oozie:x:483:478:Oozie User:/var/lib/oozie:/bin/false
hbase:x:482:477:HBase:/var/lib/hbase:/bin/false
sentry:x:481:476:Sentry:/var/lib/sentry:/sbin/nologin
impala:x:480:475:Impala:/var/lib/impala:/bin/bash
spark:x:479:474:Spark:/var/lib/spark:/sbin/nologin
hue:x:478:473:Hue:/usr/lib/hue:/bin/false
oracle:x:818:819::/home/oracle:/bin/bash
infa:x:819:821::/home/infa:/bin/bash
zabbix:x:477:472:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin


(2)、后面加一个参数-n
只输出hadoop匹配的行

cat passwd | sed  -n ‘/hadoop/p‘

[root@s80sit shell]# cat passwd | sed  -n ‘/hadoop/p‘
hdfs:x:494:489:Hadoop HDFS:/var/lib/hadoop-hdfs:/bin/bash
httpfs:x:490:485:Hadoop HTTPFS:/var/lib/hadoop-httpfs:/bin/bash
mapred:x:489:484:Hadoop MapReduce:/var/lib/hadoop-mapreduce:/bin/bash
yarn:x:487:482:Hadoop Yarn:/var/lib/hadoop-yarn:/bin/bash
kms:x:486:481:Hadoop KMS:/var/lib/hadoop-kms:/bin/bash
[root@s80sit shell]# cat passwd | sed  -n ‘/root/p‘
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

4、数据的搜寻并删除
比如删除root所在的行
cat passwd | sed   ‘/root/d‘ >> a.txt

[root@s80sit shell]# cat passwd | sed   ‘/root/d‘ >> a.txt
[root@s80sit shell]# head a.txt 
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

看结果,root所在的行被删除了!!!

5、多点编辑
一条sed命令,删除/etc/passwd第三行到末尾的数据,并把bash替换为blueshell
cat  passwd | sed -e ‘3,$d‘ -e ‘s/bash/blueshell/‘

[root@s80sit shell]#  cat  passwd | sed -e ‘3,$d‘ -e ‘s/bash/blueshell/‘
root:x:0:0:root:/root:/bin/blueshell
bin:x:1:1:bin:/bin:/sbin/nologin

6、数据的搜寻并替换
后面整理!!!!!!!!!!!!!!





本文出自 “梁小明的博客” 博客,请务必保留此出处http://7038006.blog.51cto.com/7028006/1825682

linux sed用法

标签:linux sed

原文地址:http://7038006.blog.51cto.com/7028006/1825682

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!