sed简单用法
sed是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。以下介绍的是Gnu版本的Sed 3.02。
sed用法:
sed [options] [functions]
选项[options]:
动作说明: [n1[,n2]]fuction
n1,n2 : 代表选择进行动作的行数,如 10,20[动作行为]
行为[functions]:
s : 取代, 可以直接进行取代工作,如 1,20s/old/new/g
范例一: 使用行为 a(新增)
[root@www ~]nl /etc/passwd | sed '2a 在第2行后面添加数据'
1 root:x:0:0:root:/root:/bin/bash
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
在第2行后面添加数据
范例二:使用行为 d(删除)
[root@www ~]nl /etc/passwd | sed '2,5d'
范例三:使用行为 c(取代行)
[root@www ~]nl /etc/passwd | sed '2,5c 在2-5行之间的数据被我取代了'
1 root:x:0:0:root:/root:/bin/bash
在2-5行之间的数据被我取代了
6 games:x:5:60:games:/usr/games:/bin/sh
范例四: 仅列出 /etc/passwd文件内的第 5-7 行
[root@www ~]nl /etc/passwd | sed -n '5,7p'
5 sync:x:4:65534:sync:/bin:/bin/sync
6 games:x:5:60:games:/usr/games:/bin/sh
7 man:x:6:12:man:/var/cache/man:/bin/sh
范例五: 使用sed和ifconfig只输出ip地址
5.1. 首先使用ifconfig eth0[接口名]查看详细信息
[root@www ~]ifconfig eth0
eth0 Link encap:以太网 硬件地址 00:23:ae:68:84:e4
inet 地址:192.168.10.123 广播:192.168.10.255 掩码:255.255.255.0
inet6 地址: fe80::223:aeff:fe68:84e4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
接收数据包:26597 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:17358 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:25801312 (25.8 MB) 发送字节:2114879 (2.1 MB)
中断:16
5.2. 使用grep进行过滤显示
[root@www ~]ifconfig eth0 | grep 'inet '
inet 地址:192.168.10.123 广播:192.168.10.255 掩码:255.255.255.0
5.3. 使用sed正则表达式去前面部分(^.*地址:)
[root@www ~]ifconfig eth0 | grep 'inet ' | sed 's/^.*地址://g'
192.168.10.123 广播:192.168.10.255 掩码:255.255.255.0
5.4. 使用sed正则表达式去后面部分(广播.*$)
[root@www ~]ifconfig eth0 | grep 'inet ' | sed 's/^.*地址://g' | sed 's/广播.*$//g'
192.168.10.123
5.5. 使用-e选项
[root@www ~]ifconfig eth0 | grep 'inet ' | sed -e 's/^.*地址://g' -e 's/广播.*$//g'
192.168.10.123
5.6. 使用-f选项(把参数写到sedfile文件中)
[root@www ~]ifconfig eth0 | grep 'inet ' | sed -f sedfile
192.168.10.123
范例六: /etc/manpath.config只要MAN存在的行,但是含有#在内的批注行和空白行不要
[root@www ~]cat /etc/manpath.config | grep MAN | sed 's/^#.*//g' | sed '/^$/d'
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANPATH_MAP /bin /usr/share/man
MANPATH_MAP /usr/bin /usr/share/man
MANPATH_MAP /sbin /usr/share/man
MANPATH_MAP /usr/sbin /usr/share/man
MANPATH_MAP /usr/local/bin /usr/local/man
MANPATH_MAP /usr/local/bin /usr/local/share/man
MANPATH_MAP /usr/local/sbin /usr/local/man
MANPATH_MAP /usr/local/sbin /usr/local/share/man
MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
MANPATH_MAP /usr/games /usr/share/man
MANPATH_MAP /opt/bin /opt/man
MANPATH_MAP /opt/sbin /opt/man
MANDB_MAP /usr/man /var/cache/man/fsstnd
MANDB_MAP /usr/share/man /var/cache/man
MANDB_MAP /usr/local/man /var/cache/man/oldlocal
MANDB_MAP /usr/local/share/man /var/cache/man/local
MANDB_MAP /usr/X11R6/man /var/cache/man/X11R6
MANDB_MAP /opt/man /var/cache/man/opt
范例七: 利用sed将sedtext内的每一结尾为.的换成!
sedtext的文件内容如下:
“Open Source” is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn’t fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can’t finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol ‘*’ is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world is the same with “glad”.
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let’s go.
[root@www ~]cat sedtext | sed ‘s//.$//!/g’
“Open Source” is a good mechanism to develop programs!
apple is my favorite food!
Football game is not use feet only!
this dress doesn’t fit me!
However, this dress is about $ 3183 dollars!
GNU is free air not free beer!
Her hair is very beauty!
I can’t finish the test!
Oh! The soup taste good!
motorcycle is cheap than car!
This window is clear!
the symbol ‘*’ is represented as start!
Oh! My god!
The gd software is a library for drafting programs!
You are the best is mean you are the no. 1!
The world is the same with “glad”!
I like dog!
google is the best tools for search keyword!
goooooogle yes!
go! go! Let’s go!
Author:小邪兽houshao55@gmail.com
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/31948.html