Sed – An Introduction and Tutorial by Bruce Barnett

http://www.grymoire.com/unix/sed.htmlQuick Links – NEWSed Commands: label# comment{….} Block=- pri

http://www.grymoire.com/unix/sed.html

Quick Links – NEW

<table border=”1″>

<tr>

</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;/I- Ignore Case</td>
</tr>
<tr>
<td>
<a href=”http://www.grymoire.com/unix/Sed.html#uh-9″&gt;/p- Print</td>
</tr>
<tr>
<td>
<a href=”http://www.grymoire.com/unix/Sed.html#uh-10″&gt;/w filename- Write Filename</td>
</tr>

<table border=”1″>

<tr>

</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-13″&gt;-e script (–expression=script)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-16″&gt;-f scriptfile (–file=scriptfile)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-19b”&gt;-h (–help)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-15″&gt;-n (–quiet –silent)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-19a”&gt;-V (–version)</td>
</tr>

Table of Contents

Copyright 1994,1995 Bruce Barnett and General Electric Company

Copyright 2001,2005,2007,2011,2013 Bruce Barnett

All rights reserved

You are allowed to print copies of this tutorial for your personal use,and link to this page,but you are not allowed to make electronic copies,or redistribute this tutorial in any form without permission.

Original version written in 1994 and published in the Sun Observer

Introduction to Sed

How to use sed,a special editor for modifying files automatically. If you want to write a program to make changes in a file,sed is the tool to use.

There are a few programs that are the real workhorse in the UNIX toolbox. These programs are simple to use for simple applications,yet have a rich set of commands for performing complex actions. Don’t let the complex potential of a program keep you from making use of the simpler aspects. I’ll start with the simple concepts and introduce the advanced topics later on.When I first wrote this,most versions of sed did not allow you to place comments inside the script. Lines starting with the ‘#’ characters are comments. Newer versions of sed may support comments at the end of the line as well.

Sedis the ultimatestreameditor. If that sounds strange,picture a stream flowing through a pipe. Okay,you can’t see a stream if it’s inside a pipe. That’s what I get for attempting a flowing analogy. You want literature,read James Joyce.

Anyhow,sedis a marvelous utility. Unfortunately,most people never learn its real power. The language is very simple,but the documentation is terrible. The Solaris on-line manual pages forsedare five pages long,and two of those pages describe the 34 different errors you can get. A program that spends as much space documenting the errors than it does documenting the language has a serious learning curve.

Do not fret!It is not your fault you don’t understandsed. I will coversedcompletely. But I will describe the features in the order that I learned them. I didn’t learn everything at once. You don’t need to either.

Sedhas several commands,but most people only learn the substitute command:s. The substitute command changes all occurrences of the regular expression into a new value. A simple example is changing “day” in the “old” file to “night” in the “new” file:

sed s/day/night/ new

Or another way (for UNIX beginners),

sed s/day/night/ old >new

and for those who want to test this:

echo day | sed s/day/night/ 

This will output “night”.

I didn’t put quotes around the argument because this example didn’t need them. If you read my earlier tutorial,you would understand why it doesn’t need quotes. However,I recommend you do use quotes. If you have meta-characters in the command,quotes are necessary. And if you aren’t sure,it’s a good habit,and I will henceforth quote future examples to emphasize the “best practice.” Using the strong (single quote) character,that would be:

sed 's/day/night/' new

I must emphasize that the sed editor changes exactly what you tell it to. So if you executed

echo Sunday | sed 's/day/night/'

This would output the word “Sunnight” because sed found the string “day” in the input.

Another important concept is that sed is line oriented. Suppose you have the input file:

one two three,one two three
four three two one
one hundred

and you used the command

sed 's/one/ONE/' 

The output would be

ONE two three,one two three
four three two ONE
ONE hundred

Note that this changed "one" to "ONE" once on each line. The first line had "one" twice,but only the first occurrence was changed. That is the default behavior. If you want something different,you will have to use some of the options that are available. I'll explain them later.

So let's continue.

There are four parts to this substitute command:

s    Substitute command
/../../   Delimiter
one   Regular Expression Pattern Search Pattern
ONE   Replacement string

The search pattern is on the left hand side and the replacement string is on the right hand side.

We've coveredand. That's 90% of the effort needed to learn the substitute command. To put it another way,you already know how to handle 90% of the most frequent uses ofsed.There are a ... few fine points that any future sed expert should know about. (You just finished section 1. There are only 63 more sections to cover. 🙂 Oh. And you may want to bookmark this page,.... just in case you don't finish.

The character after thesis the delimiter. It is conventionally a slash,because this is whated,more,andviuse. It can be anything you want,however. If you want to change a pathname that contains a slash - say /usr/local/bin to /common/bin - you could use the backslash to quote the slash:

sed 's/\/usr\/local\/bin/\/common\/bin/' new

Gulp. Some call this a 'Picket Fence' and it's ugly. It is easier to read if you use an underline instead of a slash as a delimiter:

sed 's_/usr/local/bin_/common/bin_' new

Some people use colons:

sed 's:/usr/local/bin:/common/bin:' new

Others use the "|" character.

sed 's|/usr/local/bin|/common/bin|' new

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

【免责声明】本站内容转载自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请提交相关链接至邮箱xwei067@foxmail.com我们将及时予以处理。

Copygight © 2017-2023 https://www.0746zz.com All Rights Reserved.永州站长网

站长:xwei067#foxmail.com(请把#换成@)

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

Sed Pattern Flags
Sed Command Line options