sed 系列: 分支操作

无条件分支语法
$ sed ':label command(s) b label'
:label - specification of label.
commands - Any sed command(s)
label - Any Name for the label
b label – jumps to the label with out checking any conditions. If label is not specified, then jumps to the end of the script.


条件分支语法
$ sed ':label command(s) t label'
:label - specification of label.
commands - Any sed command(s)
label - Any Name for the label
t label – jumps to the label only if the last substitute command modified the pattern space. If label is not specified, then jumps to the end of the script.


创建示例文件
$ cat thegeekstuff.txt
Linux
Administration
Scripting
Tips and Tricks
Windows
Administration
Database
Administration of Oracle
Administration of Mysql
Security
Network
Online\
Security
Productivity
Google Search\
Tips
"Web Based Time Tracking,
Web Based Todo list and
Reduce Key Stores etc"

无条件分支示例
示例1:替换整个文件的第一次匹配模式
$ sed '/Administration/{
s/Administration/Supervision/
:loop
n
b loop
}' thegeekstuff.txt
Linux
Supervision
Scripting
Tips and Tricks
Windows
Administration
Database
Administration of Oracle
Administration of Mysql
Security
Network
Online\
Security
Productivity
Google Search\
Tips
"Web Based Time Tracking,
Web Based Todo list and
Reduce Key Stores etc"

In the above sed command, it just read line by line and prints the pattern space till Administration occurs.
Once Administration occurs, substitute Administration to Supervision (only single occurrence, note that no ‘g’ flag in substitution).
Once the first occurrence has been replaced, just read the remaining file content and print.
“n” is a sed command which prints the pattern space and overwrite it with next line.
Used “loop” as a label. “n” prints the current line and overwrite pattern space with the next line. b loop jumps to the :loop again. So this loop prints the remaining content of thegeekstuff.txt.

示例2:移除所遇的""陪陪模式之间的内容,在我们的文件里有三行内容
$sed -e ':loop
> $!{
> N
> /\n$/!b loop
> }
> s/\"[^\"]*\"//g' thegeekstuff.txt
Linux
Administration
Scripting
Tips and Tricks
Windows
Administration
Database
Administration of Oracle
Administration of Mysql
Security
Network
Online\
Security
Productivity
Google Search\
Tips

$
示例3:移除html标签
$ cat index.html
<html><body>
<table
border=2><tr><td valign=top
align=right>1.</td>
<td>Line 1 Column 2</
td>
</table>
</body></html>
下面示例移除所遇html标签
$sed '/</{
> :loop
> s/<[^<]*>//g
> /</{
> N
> b loop
> }
> }' index.html

1.
Line 1 Column 2

Each time find a line contains ‘<’, first remove all HTML tags of that line.
If now the pattern space contains ‘<’, this implies a multi-line tag. Now repeat the following loop:
Join next line
Remove all HTML tags until no single ‘<’ exists
When no ‘<’ exists in the pattern space, we print it out and start a new cycle.
思考下面的和上面的有什么不同
$sed '/</{
> :loop
> s/<[^<]*>//g
> N
> b loop }
> ' index.html

1.
Line 1 Column 2

条件分支示例
示例1:如果一行以\结尾,那么,把它下一行的内容连接到这一行
$sed '
> :loop
> /\\$/N
> s/\\\n */ /
> t loop' thegeekstuff.txt
Linux
Administration
Scripting
Tips and Tricks
Windows
Administration
Database
Administration of Oracle
Administration of Mysql
Security
Network
Online Security
Productivity
Google Search Tips
"Web Based Time Tracking,
Web Based Todo list and
Reduce Key Stores etc"

Check if the line ends with the backslash (/\\$/), if yes, read and append the next line to pattern space, and substitute the \ at the end of the line and number of spaces followed by that, with the single space.
If the substitution is success repeat the above step. The branch will be executed only if substitution is success.
Conditional branch mostly used for recursive patterns.
示例2:替换每行的开始空格为+
$sed '
> s/^ */&\n/
> :loop
> s/^\n//;s/ \n/\n+/
> t loop' thegeekstuff.txt
Linux
++++++++Administration
++++++++Scripting
++++++++++++++++Tips and Tricks
Windows
++++++++Administration
Database
++++++++Administration of Oracle
++++++++Administration of Mysql
Security
++++++++Network
+++++++++++++++++Online\
++++++++Security
Productivity
++++++++Google Search\
++++++++Tips
++++++++"Web Based Time Tracking,
++++++++Web Based Todo list and
++++++++Reduce Key Stores etc"

Seperate all the leading spaces and other characters of a line with a newline character.
Now replace space and newline with newline and +. So from right to left space will be replaced by + and newline will be moved left for one character.
At last in the beginning of the line \n will be there, so remove that new line.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值