加入收藏 | 设为首页 | 会员中心 | 我要投稿 51站长网 (https://www.51zhanzhang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
站内搜索:
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-25 01:43:44 所属栏目:Linux 来源:网络整理
导读:http://www.grymoire.com/unix/sed.html Quick Links - NEW table border="1" tr Sed Pattern Flags /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-6"gt;/g?- Global/td /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-10a"gt;

Occasionally one wishes to use a new line character in a sed script. Well,this has some subtle issues here. If one wants to search for a new line,one has to use "n." Here is an example where you search for a phrase,and delete the new line character after that phrase - joining two lines together.

(echo a;echo x;echo y) | sed '/x$/ {
N
s:xn:x:
}'

which generates

a
xy

However,if you are inserting a new line,don't use "n" - instead insert a literal new line character:

(echo a;echo x;echo y) | sed 's:x:X
:'

generates

a
X

y

So far we have talked about three concepts of?sed: (1) The input stream or data before it is modified,(2) the output stream or data after it has been modified,and (3) the pattern space,or buffer containing characters that can be modified and send to the output stream.

There is one more "location" to be covered: the?hold buffer?or?hold space. Think of it as a spare pattern buffer. It can be used to "copy" or "remember" the data in the pattern space for later. There are five commands that use the hold buffer.

The "x" command eXchanges the pattern space with the hold buffer. By itself,the command isn't useful. Executing the?sed?command

sed 'x'

as a filter adds a blank line in the front,and deletes the last line. It looks like it didn't change the input stream significantly,but the?sed?command is modifying every line.

The hold buffer starts out containing a blank line. When the "x" command modifies the first line,line 1 is saved in the hold buffer,and the blank line takes the place of the first line. The second "x" command exchanges the second line with the hold buffer,which contains the first line. Each subsequent line is exchanged with the preceding line. The last line is placed in the hold buffer,and is not exchanged a second time,so it remains in the hold buffer when the program terminates,and never gets printed. This illustrates that care must be taken when storing data in the hold buffer,because it won't be output unless you explicitly request it.

(编辑:ASP站长)

【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

</tr>
<tr>
<td>AB</td>
<td>CD</td>
<td>N</td>
<td>-</td>
<td>ABnCD</td>
<td>EF</td>

</tr>
<tr>
<td>AB</td>
<td>CD</td>
<td>d</td>
<td>-</td>
<td>-</td>
<td>EF</td>

</tr>
<tr>
<td>AB</td>
<td>CD</td>
<td>D</td>
<td>-</td>
<td>-</td>
<td>EF</td>

</tr>
<tr>
<td>AB</td>
<td>CD</td>
<td>p</td>
<td>AB</td>
<td>AB</td>
<td>CD</td>

</tr>
<tr>
<td>AB</td>
<td>CD</td>
<td>P</td>
<td>AB</td>
<td>AB</td>
<td>CD</td>

</tr>
<tr>
<td colspan="6">?</td>

</tr>
<tr>
<td>ABnCD</td>
<td>EF</td>
<td>n</td>
<td></td>
<td>EF</td>
<td>GH</td>

</tr>
<tr>
<td>ABnCD</td>
<td>EF</td>
<td>N</td>
<td>-</td>
<td>ABnCDnEF</td>
<td>GH</td>

</tr>
<tr>
<td>ABnCD</td>
<td>EF</td>
<td>d</td>
<td>-</td>
<td>EF</td>
<td>GH</td>

</tr>
<tr>
<td>ABnCD</td>
<td>EF</td>
<td>D</td>
<td>-</td>
<td>CD</td>
<td>EF</td>

</tr>
<tr>
<td>ABnCD</td>
<td>EF</td>
<td>p</td>
<td>ABnCD</td>
<td>ABnCD</td>
<td>EF</td>

</tr>
<tr>
<td>ABnCD</td>
<td>EF</td>
<td>P</td>
<td>AB</td>
<td>ABnCD</td>
<td>EF</td>

</tr>

相关内容
未处理完善
    无相关信息
未处理完善