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

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-29 08:45:10 所属栏目: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;

A "-e" isn't needed in the earlier examples because?sed?knows that there must always be one command. If you give?sed?one argument,it must be a command,and?sed?will edit the data read from standard input.

The long argument version is

sed --expression='s/a/A/' --expression='s/b/B/' new

Also see?

You can specify files on the command line if you wish. If there is more than one argument to?sed?that does not start with an option,it must be a filename. This next example will count the number of lines in three files that don't begin with a "#:"

sed 's/^#.*//'  f1 f2 f3 | grep -v '^$' | wc -l

Let's break this down into pieces. The?sed?substitute command changes every line that starts with a "#" into a blank line.?Grep?was used to filter out (delete) empty lines.?Wc?counts the number of lines left.?Sed?has more commands that make?grep?unnecessary. And?grep -c?can replace?wc -l. I'll discuss how you can duplicate some ofgrep's functionality later.

Of course you could write the last example using the "-e" option:

sed -e 's/^#.*//'  f1 f2 f3 | grep -v '^$' | wc -l

There are two other options to?sed.

The "-n" option will not print anything unless an explicit request to print is found. I mentioned the "/p" flag to the substitute command as one way to turn printing back on. Let me clarify this. The command

sed  's/PATTERN/&/p' file

acts like the?cat?program if PATTERN is not in the file: e.g. nothing is changed. If PATTERN is in the file,then each line that has this is printed twice. Add the "-n" option and the example acts like grep:

sed -n 's/PATTERN/&/p' file

Nothing is printed,except those lines with PATTERN included.

The long argument of the -n command is either

sed --quiet 's/PATTERN/&/p' file

or

sed --silent 's/PATTERN/&/p' file

Sed?has the ability to specify which lines are to be examined and/or modified,by specifying??before the command. I will just describe the simplest version for now - the /PATTERN/ address. When used,only lines that match the pattern are given the command after the address. Briefly,when used with the /p flag,matching lines are printed twice:

sed '/PATTERN/p' file

And of course PATTERN is any regular expression.

If you want to duplicate the functionality of grep,combine the -n (noprint) option with the /p print flag:

sed -n '/PATTERN/p' file

If you have a large number of?sed?commands,you can put them into a file and use

sed -f sedscript new

where?sedscript?could look like this:

# sed comment - This script changes lower case vowels to upper case
s/a/A/g
s/e/E/g
s/i/I/g
s/o/O/g
s/u/U/g

When there are several commands in one file,each command must be on a separate line.

The long argument version is

sed --file=sedscript new

Also see?

If you have many commands and they won't fit neatly on one line,you can break up the line using a backslash:

sed -e 's/a/A/g' 
    -e 's/e/E/g' 
    -e 's/i/I/g' 
    -e 's/o/O/g' 
    -e 's/u/U/g'  new

You can have a large,multi-line?sed?script in the C shell,but you must tell the C shell that the quote is continued across several lines. This is done by placing a backslash at the end of each line:

#!/bin/csh -f
sed 's/a/A/g  
s/e/E/g 
s/i/I/g 
s/o/O/g 
s/u/U/g'  new

The Bourne shell makes this easier as a quote can cover several lines:

#!/bin/sh
sed '
s/a/A/g 
s/e/E/g 
s/i/I/g 
s/o/O/g 
s/u/U/g'  new

The -v option will print the version of sed you are using. The long argument of the command is

sed --version

The -h option will print a summary of the sed commands. The long argument of the command is

sed --help

Another way of executing?sed?is to use an interpreter script. Create a file that contains:?#!/bin/sed -fs/a/A/gs/e/E/gs/i/I/gs/o/O/gs/u/U/g

Click here to get file:?If this script was stored in a file with the name "CapVowel" and was executable,you could use it with the simple command:

CapVowel new

Sed?comments are lines where the first non-white character is a "#." On many systems,?sed?can have only one comment,and it must be the first line of the script. On the Sun (1988 when I wrote this),you can have several comment lines anywhere in the script. Modern versions of Sed support this. If the first line contains exactly "#n" then this does the same thing as the "-n" option: turning off printing by default. This could not done with a?sed?interpreter script,because the first line must start with "#!/bin/sed -f" as I think "#!/bin/sed -nf" generated an error. It works as I write this update in 2008. Note that "#!/bin/sed -fn" does not work because sed thinks the filename of the script is "n". However,

"#!/bin/sed -nf" 

does work.

(编辑:新余站长网)

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

热点阅读