注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

网易杭州 QA Team

务实 专注 分享 做有态度的QA

 
 
 
 
 

日志

 
 

Sed一些简单操作  

来自李辉   2015-12-18 00:05:07|  分类: 语言类 |举报 |字号 订阅

  下载LOFTER 我的照片书  |

在写各种script的时候,或者系统命令的时候,总少不了文本处理,天天在各种语言之间环绕,有些常用的处理方法可以总结下,以免生疏

LiHui@GodLike ~/work
$ cat lihui.txt
##################################
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1:将某行或者某些行打印出来,-n配合p

LiHui@GodLike ~/work
$ sed -n '5p' lihui.txt
onetwothree

LiHui@GodLike ~/work
$ sed -n '$p' lihui.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LiHui@GodLike ~/work
$ sed -n '2, 9p' lihui.txt
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}

LiHui@GodLike ~/work
$ sed -n 'p' lihui.txt
##################################
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2:删除某行或者某些行,带上d

LiHui@GodLike ~/work
$ sed '3d' lihui.txt
##################################
#number
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LiHui@GodLike ~/work
$ sed '2, $d' lihui.txt
##################################

3:全部匹配输出满足匹配内容的行,正则表达式匹配即可

LiHui@GodLike ~/work
$ sed -n '/one/p' lihui.txt
onetwothree
1:one 2:two 3:three
{"1":"one", "2":"two", "3":"three"}

LiHui@GodLike ~/work
$ sed -n '/\#/p' lihui.txt
##################################
#number
#word
#match
#json

4:匹配出关心的内容,然后需要二次处理进行替换,通过s和g

LiHui@GodLike ~/work
$ sed -n '/one/p' lihui.txt | sed 's/1/100/g'
onetwothree
100:one 2:two 3:three
{"100":"one", "2":"two", "3":"three"}

LiHui@GodLike ~/work
$ sed -n '/\#/p' lihui.txt | sed 's/\#/\?/g'
??????????????????????????????????
?number
?word
?match
?json

5:指定的位置,或者指定内容后面添加行,通过a

LiHui@GodLike ~/work
$ sed '1a How are you' lihui.txt
##################################
How are you
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LiHui@GodLike ~/work
$ sed '1, 2a How are you' lihui.txt
##################################
How are you
#number
How are you
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LiHui@GodLike ~/work
$ sed '1a How\nare\nyou' lihui.txt
##################################
How
are
you
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LiHui@GodLike ~/work
$ sed '/json/a How are you' lihui.txt
##################################
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
How are you
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

6:直接修改文件内容,全剧替换,而不是将处理的信息打印出来,-i

LiHui@GodLike ~/work
$ sed -i 's/\#/%/g' lihui.txt

LiHui@GodLike ~/work
$ cat lihui.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%number
123
%word
onetwothree
%match
1:one 2:two 3:three
%json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LiHui@GodLike ~/work
$ sed -i '/%/d' lihui.txt

LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{"1":"one", "2":"two", "3":"three"}

LiHui@GodLike ~/work
$ sed -i '/}/a From lihui' lihui.txt

LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{"1":"one", "2":"two", "3":"three"}
From lihui

LiHui@GodLike ~/work
$ sed -i '/}/ s/\"//g' lihui.txt

LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{1:one, 2:two, 3:three}
From lihui

7:最后有一点要注意的,这里的sed命令都是在命令行执行的,要修改的内容都是直接写出来,假如是通过shell脚本来替换,一定要注意单引号和双引号

下面这个例子,可以看到shell脚本并没有达到one=》four的替换

LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{1:one, 2:two, 3:three}
From lihui

LiHui@GodLike ~/work
$ cat hello.sh
#!/bin/bash

one=one
four=four
myfile=lihui.txt

sed -i 's/$one/$four/g' $myfile

LiHui@GodLike ~/work
$ sh hello.sh

LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{1:one, 2:two, 3:three}
From lihui

但是将shell脚本里的单引号修改成双引号,就大功告成了,也就是shell里,跟perl是一样的,双引号才能变量内插,单引号还是当作字符串的

LiHui@GodLike ~/work
$ cat hello.sh
#!/bin/bash

one=one
four=four
myfile=lihui.txt

sed -i "s/$one/$four/g" $myfile

LiHui@GodLike ~/work
$ sh hello.sh

LiHui@GodLike ~/work
$ cat lihui.txt
123
fourtwothree
1:four 2:two 3:three
{1:four, 2:two, 3:three}
From lihui

而命令行里,通过下面sed命令显然是可以直接替换的

$ sed -i 's/one/four/g' lihui.txt
  评论这张
 
阅读(144)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016