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

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-25 14:11:33 所属栏目: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;

You can execute a branch if a pattern is found. You may want to execute a branch only if a substitution is made. The command "t label" will branch to the label if the last substitute command modified the pattern space.

One use for this is recursive patterns. Suppose you wanted to remove white space inside parenthesis. These parentheses might be nested. That is,you would want to delete a string that looked like "( ( ( ())) )." The?sed?expressions

sed 's/([ ^I]*)/g'

would only remove the innermost set. You would have to pipe the data through the script four times to remove each set or parenthesis. You could use the regular expression

sed 's/([ ^I()]*)/g'

but that would delete non-matching sets of parenthesis. The "t" command would solve this:

#!/bin/sh
sed '
:again
    s/([ ^I]*)//
    t again
'

An earlier version had a 'g' after the 's' expression. This is not needed.

Click here to get file:?

The 'l' command will print the pattern space in an unambiguous form. Non-printing characters are printed in a C-style escaped format.

This can be useful when debugging a complex multi-line sed script.

There is one way to add comments in a?sed?script if you don't have a version that supports it. Use the "a" command with the line number of zero:?

#!/bin/sh
sed '
/begin/ {
0i
    This is a comment
    It can cover several lines
    It will work with any version of sed
}'

Click here to get file:?

There is one more?sed?command that isn't well documented. It is the ";" command. This can be used to combined several?sed?commands on one line. Here is the?grep4script I described earlier,but without the comments or error checking and with semicolons between commands:?#!/bin/shsed -n ''/$1/' !{;H;x;s/^.*n(.*n.*)$/1/;x;}'/$1/' {;H;n;H;x;p;a---}'

Click here to get file:?

Yessireebob! Definitely character building. I think I have made my point. As far as I am concerned,the only time the semicolon is useful is when you want to type thesed?script on the command line. If you are going to place it in a script,format it so it is readable. I have mentioned earlier that many versions of?sed?do not support comments except on the first line. You may want to write your scripts with comments in them,and install them in "binary" form without comments. This should not be difficult. After all,you have become a?sed?guru by now. I won't even tell you how to write a script to strip out comments. That would be insulting your intelligence. Also - some operating systems do NOT let you use semicolons. So if you see a script with semicolons,and it does not work on a non-Linux system,replace the semicolon with a new line character. (As long as you are not using csh/tcsh,but that's another topic.

In the earlier scripts,I mentioned that you would have problems if you passed an argument to the script that had a slash in it. In fact,regular expression might cause you problems. A script like the following is asking to be broken some day:

#!/bin/sh
sed 's/'"$1"'//g'

If the argument contains any of these characters in it,you may get a broken script: "/.*[]^$" For instance,if someone types a "/" then the substitute command will see four delimiters instead of three. You will also get syntax errors if you provide a "]" without a "]". One solution is to have the user put a backslash before any of these characters when they pass it as an argument. However,the user has to know which characters are special.?Another solution is to add a backslash before each of those characters in the script

#!/bin/sh
arg=`echo "$1" | sed 's:[][^$.*/]:\&:g'`
sed 's/'"$arg"'//g'

Click here to get file:?If you were searching for the pattern "^../," the script would convert this into "^../" before passing it to?sed.

Dealing with binary characters can be trick,expecially when writing scripts for people to read. I can insert a binary character using an editor like EMACS but if I show the binary character,the terminal may change it to show it to you.

The easiest way I have found to do this in a script in a portable fashion is to use the tr(1) command. It understands octal notations,and it can be output into a variable which can be used.

Here's a script that will replace the string "ding" with the ASCII bell character:

#!/bin/sh
BELL=`echo x | tr 'x' '07'`
sed "s/ding/$BELL/"

Please note that I used double quotes. Since special characters are interpreted,you have to be careful when you use this mechanism.

As I promised earlier,here is a table that summarizes the different commands. The second column specifies if the command can have a range or pair of addresses or a single address or pattern. The next four columns specifies which of the four buffers or streams are modified by the command. Some commands only affect the output stream,others only affect the hold buffer. If you remember that the pattern space is output (unless a "-n" was given to?sed),this table should help you keep track of the various commands.

Command (编辑:晋中站长网)

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

Address or Range Modification toInput Stream Modification toOutput Stream Modification toPattern Space Modification toHold Buffer