Linux中的sed命令
sed - stream editor for filtering and transforming text
流编辑器的过滤和转换文本
sed [-nerf] [动作]
参数:
-i 修改源文件 危险
-e 直接在命令行模式上执行sed的动作编辑
-f 直接将sed的动作写在一个文件内,-f filename 则可以执行filename内的sed动作
-r :使用扩展的正则表达式
使用扩展的正则表达式需要“//”正斜线
[root@lamp scripts]# cat sed11.txt [root@lamp scripts]# sed -nr '/2|3/p' sed11.txt //打印包含2或3的行 [root@lamp scripts]# sed -nr '/2|3$/p' sed11.txt //打印以2或3结尾的行,但是打印的是以包含2和以3结尾的行 [root@lamp scripts]# sed -nr '/(2|3)$/p' sed11.txt //这个才是正确的以2或3结尾的行 [root@lamp scripts]#
-n 静默模式,默认的sed中所有来自stdin的数据一般都会被列出到屏幕上,但如果加上-n之后,则只有经过sed特殊处理的那一行才会被列出来。
[root@mysql tmp]# sed "/^a/p" sed.txt //将以”a“开头的行打印出来,可以看到b也被打印出来了,并且a被打印了两遍,一遍是sed默认就是将模式空间中的内容打印出来,第二遍是p参数又打印了一遍 a a b [root@mysql tmp]# sed -n "/^a/p" sed.txt //使用-n参数,只讲匹配到的内容打印出来 a [root@mysql tmp]#
"n"和“i”不能同时使用
[root@lamp scripts]# sed -ni "s#Port 22#Port 52113#p" /tmp/sshd_config [root@lamp scripts]# cat /tmp/sshd_config //啥都没有了,经测试只有“-ni”会发生这种情况,“-in”则不会 #Port [root@lamp scripts]#
动作说明:[n1,n2] function
1.LineNumber
精确匹配的行
2.StarLine, +N
从startline开始,到之后的n行结束,一共是n+1行
3.StartlLine~n
从指定行开始指定步长
[root@lamp scripts]# cat sed.txt. one two three four five [root@lamp scripts]# sed -n '1~2p' sed.txt. //从第一行开始只有1,3,5行被打印出来 one three five [root@lamp scripts]#
4.StartLine,EndLine
$ 表示最后一行
[root@mysql tmp]# sed -n "1,2p" /etc/passwd //打印1-2行的内容 root:x:::root:/root:/bin/bash bin:x:::bin:/bin:/sbin/nologin [root@mysql tmp]# sed -n "30,\$p" /etc/passwd //答应30到最后一行的内容 mysql:x::::/home/mysql:/sbin/nologin apache:x:::Apache:/var/www:/sbin/nologin [root@mysql tmp]# sed -n "30p" /etc/passwd //打印第30行的内容 mysql:x::::/home/mysql:/sbin/nologin
5./匹配模式/ 支持正则表达式
如:/^root/
6./partern/,/partern/
第一次被partern1匹配到的行开始,至第一次被partern2匹配到的行结束, 这中间的所有行
[root@mysql tmp]# sed -n "/bash$/p" /etc/passwd //打印以bash结尾的行 root:x:::root:/root:/bin/bash along:x::::/home/along:/bin/bash [root@mysql tmp]# sed -n "/^rpc/p" /etc/passwd //打印以rpc开头的行 rpc:x:::Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin rpcuser:x:::RPC Service User:/var/lib/nfs:/sbin/nologin [root@mysql tmp]# sed -n "/^\(.*\)root/p" /etc/passwd //打印以任意字符开头,中间有root的行 root:x:::root:/root:/bin/bash operator:x:::operator:/root:/sbin/nologin [root@mysql tmp]#
function
= 打印行号
[root@lamp scripts]# sed -n '1~2=' sed.txt. //打印1 3 5行号
[root@lamp scripts]# sed -n '1~2=;p' sed.txt. //不仅打印行号,内容也打印,但是结果显示是将所有的内容都打印了,因为“p”操作前边什么都没有指定,默认就是将所有的打印
one
two
three
four
five
[root@lamp scripts]# sed -n '1~2{=;p}' sed.txt. //针对同一内容的不同操作应该使用“{}”括起来,并且不同的操作之间应该使用分号隔开
one
three
five
[root@lamp scripts]#
d:删除符合条件的行
p:打印符合条件的行
使用此命令的话,指定的内容会打印两次,这是因为sed命令默认的就是 将模式空间中的内容打印出来,而p也是打印的意思,所以会重复打印
a \string : 在指定的行后面追加新的行,内容是string
a的意思append追加的意思,即在指定行的后边追加内容
[root@mysql tmp]# cat sed.txt a b [root@mysql tmp]# a="c" //设置一个变量a [root@mysql tmp]# sed "2a $a" sed.txt //在第二行后边追加变量a的值 a b c [root@mysql tmp]# sed "2a \$a" sed.txt //特殊符号需要转义 a b $a [root@mysql tmp]#
i \string: 在指定的行前面添加新的行,内容是string
”i“就是insert的意思
[root@mysql tmp]# cat sed.txt a b [root@mysql tmp]# sed '1i 1' sed.txt //在指定行的前边添加一行 a b [root@mysql tmp]#
r File:将指定文件的内容添加至符合条件的行处
[root@mysql tmp]# cat sed.txt a d [root@mysql tmp]# cat sed1.txt b c [root@mysql tmp]# sed "2r ./sed1.txt" sed.txt //将sed1.txt的内容在sed.txt的第二行开始添加 a d b c [root@mysql tmp]#
w File:将指定范围的内容添加至指定的文件中去
[root@mysql tmp]# sed -n "30,\$w ./a.txt" /etc/passwd //将passwd文件的最后两行写入到a.txt文件中去 [root@mysql tmp]# cat a.txt mysql:x::::/home/mysql:/sbin/nologin apache:x:::Apache:/var/www:/sbin/nologin [root@mysql tmp]#
s /partern1/partern2/修饰符 查找替换 默认只替换第一次被匹配到的字符串
不仅可使用s ///也可以使用s ### ,s @@@等,s 命令也可以支持后向引用
将garnett替换成kobe,html替换成HTML [root@lamp ~]# echo garnett-linux.html|sed "s#garnett\(.*\)html#kobe\1HTML#g" kobe-linux.HTML [root@lamp ~]#
修饰符:
g:全局替换
[root@lamp scripts]# head /etc/passwd|sed "s#[a-z]##" //发现只是将每一行的第一个字母替换成空 ,因为”s“默认只替换第一次被匹配到的字符串 oot:x:::root:/root:/bin/bash, ::bin:/bin:/sbin/nologin aemon:x:::daemon:/sbin:/sbin/nologin dm:x:::adm:/var/adm:/sbin/nologin p:x:::lp:/var/spool/lpd:/sbin/nologin ync:x:::sync:/sbin:/bin/sync hutdown:x:::shutdown:/sbin:/sbin/shutdown alt:x:::halt:/sbin:/sbin/halt ail:x:::mail:/var/spool/mail:/sbin/nologin ucp:x:::uucp:/var/spool/uucp:/sbin/nologin [root@lamp scripts]# head /etc/passwd|sed "s#[a-z]##g" //加上"g"参数即可,可以重复替换 :::::/:// :::::/:// :::::/:// ::::://:// :::::///:// :::::/:// :::::/:// :::::/:// :::::///:// :::::///:// [root@lamp scripts]#
i:忽略大小写
实战:
使用sed命令取ip地址
[root@lamp scripts]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr :0C:::CD:
inet addr:192.168.220.129 Bcast:192.168.220.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe17:cd98/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: ( (208.2 KiB)
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bc(.*)#\2#gp"
192.168.220.129
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bcast:(.*) (.*)#\2#gp"
192.168.220.129
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bcast:(.*) (.*)#\2\3#gp"
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bcast:(.*) (.*)#\2 \3#gp"
192.168.220.129 192.168.220.255
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bcast:(.*) (.*)#\2\t\3#gp"
192.168.220.129 192.168.220.255
[root@lamp scripts]#
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bcast:(.*) (.*)#ping \2#gp"
ping 192.168.220.129
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*) Bcast:(.*) (.*)#ping \2#gp"|bash
PING () bytes of data.
bytes from ttl= time=0.032 ms
bytes from ttl= time=0.033 ms
bytes from ttl= time=0.045 ms
bytes from ttl= time=0.040 ms
^C
--- 192.168.220.129 ping statistics ---
packets transmitted, received, % packet loss, time 8225ms
rtt min/avg/max/mdev = 0.022/0.038/0.049/0.008 ms
[root@lamp scripts]#
Linux中的sed命令的更多相关文章
- Linux中使用sed命令或awk命令修改常规配置文件
一.方案: Linux中使用sed命令或awk命令修改常规配置文件 二.步骤: 1.假设有一个a.txt,内容如下: #!/bin/bash aa= bbb= ccc= #ddd= 2.如果想要把里面 ...
- Linux中使用sed命令替换字符串小结
sed替换的基本语法为: sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义,但是单引号”‘”是没有办法用反斜线”\”转义的 ...
- 【转】linux中的sed命令
转自:http://www.cnblogs.com/shineshqw/articles/1978122.html 功能说明: 利用script来处理文本文件. 语 法:sed [-hnV][-e&l ...
- Linux实战教学笔记12:linux三剑客之sed命令精讲
第十二节 linux三剑客之sed命令精讲 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件 ...
- linux三剑客之sed命令
一.前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件等等.如果我们相对这些文件进行一些编辑查询等操作时,我们可能会想到一些vi,vim,cat,more等命令.但是这些命令 ...
- day14 linux三剑客之sed命令
day14 linux三剑客之sed命令 sed命令 Sed 主要用来自动编辑一个或多个文件.简化对文件的反复操作.编写转换程序等. sed(流式编辑器) : sed主要用来修改文件. 1.sed命令 ...
- Linux中的历史命令
Linux中的历史命令一般保存在用户 /root/.bash_history history 选项 历史命令保存文件夹 选项 -c:清空历史命令 -w :把缓存中的历史命令写入历 ...
- 关于XShell的常见使用和设置以及Linux中的常见命令.
本文部分转自:http://sundful.iteye.com/blog/704079 和 http://www.vckai.com/p/5 有时候在XShell中操作的一些命令傻傻的分不清这个命令到 ...
- linux中的strings命令简介2
摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 之前我们聊过linux strings的用法和用 ...
随机推荐
- 用手机或外部设备在同一局域网下访问虚拟主机wampsever的方法版本号是2.4.9
1,首先在虚拟服务器电脑上可以打开http://localhost/ 2,在外部设备访问时报错为:You don't have permission to access / in on this se ...
- 转:微信开发之使用java获取签名signature(贴源码,附工程)
微信开发之使用java获取签名signature(贴源码,附工程) 标签: 微信signature获取签名 2015-12-29 22:15 6954人阅读 评论(3) 收藏 举报 分类: 微信开发 ...
- Vue.js 系列教程 5:动画
原文:intro-to-vue-5-animations 译者:nzbin 译者的话:经过两周的努力,终于完成了这个系列的翻译,由于时间因素及个人水平有限,并没有详细的校对,其中仍然有很多不易理解的地 ...
- 对synchronized关键字的理解
先看两个线程同时访问一个对象的例子. public class Account { private String accountNo; private double balance; public A ...
- PHP连接数据库:封装成类
php连接数据库,操作他增删改查等操作,其中要多次连接数据库,每个页面也需要连接数据库,更改数据会及其麻烦: 为了便于数据库的更改,我们可以把固定的那几句话封装成类,这样虽然代码量也差不多,但是有利于 ...
- Android 退出多Activity的application的方式
在开发过程中,我们常常需要一个退出功能,来退出该应用的所有Activity.下面,我们列举一些退出应用的几种方式.以下用的源码点击查看源码地址 欢迎star,欢迎fork 利用ActivityCont ...
- 【转】CXF+Spring+Eclipse简明示例
多系统(异构系统)进行交互时,一种良好的方式便是调用Web Service,本示例基于Apache组织的CXF,为了方便起见特将服务端和客户端写在同一个工程下,实际项目中是不可能的,但是客户端却依赖于 ...
- JSP中三种弹出对话框的用法《转》
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- 了解 : angular controller link ng-init 顺序
controller 会先跑,接着是view 里的ng-init,最后是link (指令里的). 所有在指令里如果用link去拿$attr,会有拿不到ng-init想setup的值
- 抓包工具Wireshark的使用
WireShark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息. WireShark界面简介 启动WireShark的界面如下: 选择网卡 wireshar ...