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

蒙奇D小豌豆的博客

蒙奇D小豌豆的学习记录

 
 
 

日志

 
 

Sed  

2014-05-22 16:10:03|  分类: language |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1. format
sed 'n,m{/regex1/comd1;comd2;xxxx};n1,m1{/regex3/{/regex4/comd3;comd4;xxxx}'
# n,m n1,m1 regex* are optinal and it can be only one command

a. specail words
n,m : line n to m;(n is also okay)
;   : connect two[group] command
{}  : a group of command with same regex or lines

b. regex:
^xx: each record(line) start with xx 
xx$: each record(line) end with xx
.: one any single character, except "\n" 
[xxx]:one of the group character [0-9a-i],[acd]
[^xxx]:none one of the group character [^0-9],[^acd]
\?: the preceding character only has zero or one times. fe\?d can only match fed and fd
\+: the preceding character has more than one times. fe\+d can match fe...ed and fed
*: the prceding character has zero or one times, serval times. fe*d can match fe...ed, fed and fd
\(regex1\|regex\2): group for the two regexs
\{n,m\}: the prceding character has n to m times. fe\{2,3\}, fe\{2\}, fe\{2,\}
\<xx: xx is in the beginning of a word. 
\>xx: xx is in the end of a word

/^$/: empty record

c. command
1) a,bs/regex/str/n : replace regex with str for the n matches in the a to b lines
   a,$s/regex/str/g : replace regex with str for all the matches in the a to end lines
   s/regex/str/ng: replace regex with str for n to end matches for all lines 

2) n,m{/regex/a} str: append str after the matched line, n,m is optional, m/n can be ^(first line), $(last line)
   2a str           : append str after the 2 line

3) n,m{/regex/i} str: insert str before the matched line, n,m is optional
   2i str           : insert str before the 2 line

4) n,m{/regex/c} str: replace the matched line with str, n,m is optional
   2c str           : replace the 2 line with str

5) n,m{/regex/d}    : delete the matched line, n,m is optinal
   2i str           : delete str before the 2 line

d. &
& : represent the match value
sed 's/my/[&]/g' : change my to [my]


2. example
#replace my to Ha..'s for all matches in each line
sed "s/my/Hao Chen's/g" pets.txt

sed 's/^/#/g' pets.txt # add # in the beginning of each line

sed 's/s/S/1' my.txt # just replace the first one for each line 

sed 's/s/S/3g' my.txt # replace the three to last one for each line

# first 1-3 lines replace a, then 3 to last line replace b
sed '1,3s/my/your/g; 3,$s/This/That/g' my.txt

#insert xxx before the first line as a single line
sed "1i xxxx" pets.txt
#insert xxx before all the matched lines as a single line
sed "/aa/i xxxx" pets.txt

# append xxx after the last line as asingle line
sed "$a xxx" my.txt
# append xxxafterall the matched lines as a single line
sed "/aa/a xxxx" pets.txt

#replace xxx for the 3st line
sed "3c xxxx" pets.txt
#replace xxx for all the matched lines
sed "/aa/c xxxx" pets.txt

#delete the 3st line
sed "3d" pets.txt

#delete all the matched lines form 2 to 6 line
sed "2,6{/aa/d}" pets.txt

# a group of matches
sed '1,${/This/d;s/^ *//g}' pets.txt
  评论这张
 
阅读(73)| 评论(0)

历史上的今天

评论

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

页脚

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