---
title: "对sed1line中一些命令的理解 二"
author: "icepp"
date: "2009-03-01T10:54:20Z"
tags: []
slug: "sed1line"
draft: false
lastmod: "2009-03-01T11:18:42Z"
id: 340934
url: "https://icepp.iteye.com/blog/340934"
---

```
# insert a blank line above every line which matches "regex" 
#在每个含有字符串regex的行上插入一行空白行 
sed '/regex/{x;p;x;}'
```

 

复习下x ,p命令  
x   Exchange the contents of the hold and pattern spaces.  
P   Print the current pattern space.

 

脚本工作流程：

当读入匹配RE的那一行时，  
      pattern space:  regex  
      hold space :  blank

先执行x，此时  
      pattern space: blank  
      hold space : regex

继续执行p,空行被打印出来  
      pattern space: blank  
      hold space : regex

往下执行x  
      pattern space: regex  
      hold space : blank

到脚本底，执行默认输出regex，读入下行继续处理。

这样就在每个含有字符串regex的行上插入一行空白行。