加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (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;

Passing a word into a shell script that calls?sed?is easy if you remembered??To review,you use the single quotes to turn quoting on and off. A simple shell script that uses?sed?to emulate grep is:?#!/bin/shsed -n 's/'$1'/&/p'However - there is a problem with this script. If you have a space as an argument,the script would cause a syntax error A better version would protect from this happening:?#!/bin/shsed -n 's/'"$1"'/&/p'

Click here to get file:?If this was stored in a file called?sedgrep,you could type

sedgrep '[A-Z][A-Z]' 

This would allow sed to act as the grep command.

You can use?sed?to prompt the user for some parameters and then create a file with those parameters filled in. You could create a file with dummy values placed inside it,and use?sed?to change those dummy values. A simpler way is to use the "here is" document,which uses part of the shell script as if it were standard input:

#!/bin/sh
echo -n 'what is the value? '
read value
sed  's/XXX/'$value'/' <

Click here to get file:?When executed,the script says:

what is the value?

If you type in "123," the next line will be:

The value is 123

I admit this is a contrived example. "Here is" documents can have values evaluated without the use of sed. This example does the same thing:

#!/bin/sh
echo -n 'what is the value? '
read value
cat <

However,combining "here is" documents with?sed?can be useful for some complex cases.?Note that?sed 's/XXX/'$value'/' <

As we explore more of the commands of?sed,the commands will become complex,and the actual sequence can be confusing. It's really quite simple. Each line is read in. Each command,in order specified by the user,has a chance to operate on the input line. After the substitutions are made,the next command has a chance to operate on the same line,which may have been modified by earlier commands. If you ever have a question,the best way to learn what will happen is to create a small example. If a complex command doesn't work,make it simpler. If you are having problems getting a complex script working,break it up into two smaller scripts and pipe the two scripts together.

You have only learned one command,and you can see how powerful?sed?is. However,all it is doing is a?grep?and substitute. That is,the substitute command is treating each line by itself,without caring about nearby lines. What would be useful is the ability to restrict the operation to certain lines. Some useful restrictions might be:

  • Specifying a line by its number.
  • Specifying a range of lines by number.
  • All lines containing a pattern.
  • All lines from the beginning of a file to a regular expression
  • All lines from a regular expression to the end of the file.
  • All lines between two regular expressions.

Sed?can do all that and more. Every command in?sed?can be proceeded by an address,range or restriction like the above examples. The restriction or address immediately precedes the command:

restriction?command

The simplest restriction is a line number. If you wanted to delete the first number on line 3,just add a "3" before the command:

sed '3 s/[0-9][0-9]*//' new

Many UNIX utilities like?vi?and?more?use a slash to search for a regular expression.?Sed?uses the same convention,provided you terminate the expression with a slash. To delete the first number on all lines that start with a "#," use:

sed '/^#/ s/[0-9][0-9]*//'

I placed a space after the "/expression/" so it is easier to read. It isn't necessary,but without it the command is harder to fathom.?Sed?does provide a few extra options when specifying regular expressions. But I'll discuss those later. If the expression starts with a backslash,the next character is the delimiter. To use a comma instead of a slash,use:

sed ',^#,s/[0-9][0-9]*//'

The main advantage of this feature is searching for slashes. Suppose you wanted to search for the string "/usr/local/bin" and you wanted to change it for "/common/all/bin." You could use the backslash to escape the slash:

sed '//usr/local/bin/ s//usr/local//common/all/'

(编辑:晋中站长网)

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

热点阅读