Skip to main content

Sed - An Introduction and Tutorial

Popularity Report

Total Popularity Score: 0

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Rank

Related Lists

Bookmark History

Saved by 123 people (-25 private), first by anonymouse user on 2006-03-02


Public Comment

on 2006-08-23 by inetgate

sed入門

on 2006-11-03 by pistos

Sed an Introduction

Public Sticky notes

Anyhow, sed is 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 for sed are 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 understand sed. I will cover sed completely. 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 eithe

Highlighted by david_r_whiting

Anyhow, sed is 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 for sed are 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.

Highlighted by pklausner

Sed - An Introduction and Tutorial

Highlighted by alpachino

Anyhow, sed is 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 for sed are 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.

Highlighted by calamityfactors

How can you put the string you found in the replacement string if you don't know what it is?

Highlighted by hamming229

he solution requires the special character "&." It corresponds to the pattern found.

Highlighted by hamming229

'[0-9]*' is the first character on the line, as this matches zero of more numbers

Highlighted by yw2298

Printing with p

Another useful command is the print command: "p." If sed wasn't started with an "-n" option, the "p" command will duplicate the input. The command

sed 'p'

will duplicate every line. If you wanted to double every empty line, use:

sed '/^$/ p'

Adding the "-n" option turns off printing unless you request it. Another way of duplicating head's functionality is to print only the lines you want. This example prints the first 10 lines:

sed -n '1,10 p' <file

Sed can act like grep by combining the print operator to function on all lines that match a regular expression:

sed -n '/match/ p'

Highlighted by aowongster

The "=" command prints the current line number to standard output. One way to find out the line numbers that contain a pattern is to use:

# add line numbers first,
# then use grep,
# then just print the number
cat -n file | grep 'PATTERN' | awk '{print $1}'

The sed solution is:

sed -n '/PATTERN/ =' file

Highlighted by david_r_whiting