Shell Practice: Introduction to the sed stream editor

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Regular Expressions

Some regular expressions are used in sed. You can use them to describe many string patterns. The more of them you use in sed, the more confusing it can get with more complex statements. Here, sed scripts can often help. Some characters are valid as special shell characters as well as regex instructions. These you need to "escape" with the \ character (Table 1). The construct [ABC] means "contains A or B or C" and the construct /ABC/ means "contains exactly that string."

Table 1

Special Characters

Character Function
( Opens statement
) Ends statement
{ Opens optional statement
} Closes optional statement
[ Opens a list of characters
] Closes a list of characters
" Masks a statement in which shell variables are resolved
' Masks a statement in which shell variables not resolved
` Encloses a statement block
. Any character other than a newline
, Separates parameters, such as line items
: Sets labels (t and b command)
$ End of document, end of line or last line
& Placeholder for search patterns, included in the replacement statement
| Or (regex separator)
/ Separator in editing commands
^ Beginning of line, or negation in a search pattern
\ Escape character
! After a line number: do not output this line
* 0 Or any number of times
+ Pattern present at least once
= Output line number
\n Newline, line feed
\t Tab character

Options and Editing Commands

Confusingly, both sed and the editing commands have their own options. As is usual in Linux, these options are preceded with the - character. The editing statements take their options after them. Tables 2-4 provide an overview.

Table 2

Sed Options

Action Option
Execute command (can usually be omitted) -e
Disable data buffering -u
Treat files separately -s
Use extended regexes -r
Create backup file -i [FILEEXTENSION]
Read and execute script file -f [SCRIPTFILE]
Suppress (unaffected) text areas -n
Show version -v

Table 3

Editing Commands

Action Command
Add lines above this one i
Add lines below this one a
Output this line p
Output this line with a maximum length l [LENGTH]
Replace signs with others y
End sed q
Replace text in this line c
Delete this line d
Search and replace s

Table 4

Editing Command Options

Action Option
Output line number =
All occurrences g
Outputs modified line with the s editing command p
Write the edited line in the file w

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content