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

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-24 22:14:51 所属栏目: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;

Hardly worth the buildup. All that prose and the solution is just matching squiggles. Well,there is one complication. Since each?sed?command must start on its own line,the curly braces and the nested?sed?commands must be on separate lines.

Previously,I showed you how to remove comments starting with a "#." If you wanted to restrict the removal to lines between special "begin" and "end" key words,you could use:?

#!/bin/sh
# This is a Bourne shell script that removes #-type comments
# between 'begin' and 'end' words.
sed -n '
    /begin/,/end/ {
         s/#.*//
         s/[ ^I]*$//
         /^$/ d
         p
    }
'

Click here to get file:?These braces can be nested,which allow you to combine address ranges. You could perform the same action as before,but limit the change to the first 100 lines:?

#!/bin/sh
# This is a Bourne shell script that removes #-type comments
# between 'begin' and 'end' words.
sed -n '
    1,100 {
        /begin/,/end/ {
             s/#.*//
             s/[ ^I]*$//
             /^$/ d
             p
        }
    }
'

Click here to get file:?You can place a "!" before a set of curly braces. This inverts the address,which removes comments from all lines?except?those between the two reserved words:?

#!/bin/sh
sed '
    /begin/,/end/ !{
         s/#.*//
         s/[ ^I]*$//
         /^$/ d
         p
    }
'

Click here to get file:?

You may remember that I mentioned you can do a substitute on a pattern range,like changing "old" to "new" between a begin/end pattern:

#!/bin/sh
sed '
    /begin/,/end/ s/old/new/
'

Another way to write this is to use the curly braces for grouping:

#!/bin/sh
sed '
    /begin/,/end/ {
        s/old/new/
    }
'

I think this makes the code clearer to understand,and easier to modify,as you will see below.

If you did not want to make any changes where the word "begin" occurred,you could simple add a new condition to skip over that line:

#!/bin/sh
sed '
    /begin/,/end/ {
        /begin/n # skip over the line that has "begin" on it
        s/old/new/
    }
'

However,skipping over the line that has "end" is trickier. If you use the same method you used for "begin" then the sed engine will not see the "end" to stop the range - it skips over that as well. The solution is to do a substitute on all lines that don't have the "end" by using

#!/bin/sh
sed '
    /begin/,/end/ {
      /begin/n # skip over the line that has "begin" on it
      /end/ !{
        s/old/new/
      }  
    }
'

You may remember that the substitute command can write to a file. Here again is the example that will only write lines that start with an even number (and followed by a space):?

sed -n 's/^[0-9]*[02468] /&/w even' 

I used the "&" in the replacement part of the substitution command so that the line would not be changed. A simpler example is to use the "w" command,which has the same syntax as the "w" flag in the substitute command:?

sed -n '/^[0-9]*[02468]/ w even' 

Remember - only one space must follow the command. Anything else will be considered part of the file name. The "w" command also has the same limitation as the "w" flag: only 10 files can be opened in?sed.

There is also a command for reading files. The command

sed '$r end' out

will append the file "end" at the end of the file (address "$)." The following will insert a file after the line with the word "INCLUDE:"

sed '/INCLUDE/ r file' out

You can use the curly braces to delete the line having the "INCLUDE" command on it:?

#!/bin/sh
sed '/INCLUDE/ {
    r file
    d
}'

Click here to get file:?

(编辑:威海站长网)

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

热点阅读