加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (https://www.0555zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-29 10:07:16 所属栏目:站长百科 来源:网络整理
导读:副标题#e# 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

The "a" command appends a line after the range or pattern. This example will add a line after every line with "WORD:"?

#!/bin/sh
sed '
/WORD/ a
Add this line after every line with WORD
'

Click here to get file:?

You could eliminate two lines in the shell script if you wish:?

#!/bin/sh
sed '/WORD/ a
Add this line after every line with WORD'

Click here to get file:?

I prefer the first form because it's easier to add a new command by adding a new line and because the intent is clearer. There must not be a space after the "".

You can insert a new line before the pattern with the "i" command:?

#!/bin/sh
sed '
/WORD/ i
Add this line before every line with WORD
'

Click here to get file:?

You can change the current line with a new line.?

#!/bin/sh
sed '
/WORD/ c
Replace the current line with the line
'

Click here to get file:?

A "d" command followed by a "a" command won't work,as I discussed earlier. The "d" command would terminate the current actions. You can combine all three actions using curly braces:?

#!/bin/sh
sed '
/WORD/ {
i
Add this line before
a
Add this line after
c
Change the line to this one
}'

Click here to get file:?

Sed?ignores leading tabs and spaces in all commands. However these white space characters may or may not be ignored if they start the text following a "a," "c" or "i" command. In SunOS,both "features" are available. The Berkeley (and Linux) style sed is in /usr/bin,and the AT&T version (System V) is in /usr/5bin/.

To elaborate,the?/usr/bin/sed?command retains white space,while the?/usr/5bin/sed?strips off leading spaces. If you want to keep leading spaces,and not care about which version of?sed?you are using,put a "" as the first character of the line:

#!/bin/sh
sed '
    a
   This line starts with a tab
'

All three commands will allow you to add more than one line. Just end each line with a ":"

#!/bin/sh
sed '
/WORD/ a
Add this line
This line
And this line
'

(编辑:网站开发网_马鞍山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!