Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 0

SED Metacharacters

Description Command
Format
Example
^ Beginning-of-line /^love/ Matches all lines beginning with
love
$ End-of-line anchor /love$/ Matches all lines ending with love
. Matches one character but
not the newline character
/l..e/ Matches lines containing an l,
followed by 2 characters,
followed by an e.
* Matches zero or more /*love/ Matches lines with zero or more
spaces, followed by the pattern
love.
[] Matches a particular set of
character.
/[Ll]ove/ Matches lines containing love or
Love.
[^] Matches one character not
in the set
/[^A-KM-Z]ove/ Matches lines not containing A
through K or M through Z
followed by love.
\< Beginning-of-word anchor ^<love/ Matches lines containing word
that begins with love (supported
by vi and grep).
\> End-of-Word anchor /love\>/ Matches lines containing a word
that ends with love (supported by
vi and grep)
& Saves search string so it
can be remembered
s/love/**&**/ & represents the search string.
The string love will be replaced
with itself surrounded by
asterisks.
\(..\) Tags match characters s^(love\)able^1er/ Tags marked portion in a register
to be remembered later as number
1. To reference later, use \1 to
repeat the pattern. May use up to
nine tags, starting with the first
tag at the left-most part of the
pattern. ie, the pattern love is
saved as tag l, to be referenced
later as \1; in this example, the
search pattern consists of lovable
followed by lover (supported by
sed, vi and grep)
x\{m\}
or
x\{m,\} or
x\{m,n\}
a

Repetition of character x,
m times,
at least m times,
at least m and not more
than n times
o\{5,10\}

o\{5,\}
o\{5,10\}
Matches if line contains between
5 and 10 consecutive occurrences
of the letter o (supported by vi
and grep)
a
Not dependable on all versions of UNIX or all pattern-matching utilities; usually works
with vi and grep



SED Commands

Command
a\ Appends one or more lines of text to the current line
c\ Changes (replaces) text in the current line with new text
d deletes lines
i\ Inserts text above the current line
h Copies the contents of the pattern space to a holding buffer
H Appends the contents of the pattern space to a holding buffer
g Gets what is in the holding buffer and copies it into the pattern bugger,
overwriting what was there.
G Gets what is in the holding buffer and copies it into the pattern buffer, appending
to what was there
l lists nonprinting characters
p print lines
n Reads the next input line and starts processing the newline with the next
command rather than the first command
q Quits or exits sed
r Reads lines from a file
! Applies the command to all lines except the selected ones.
s Substitutes one string for another

Substitution Flags:
g Globally substitutes on a line
p Prints lines
w Writes lines out to a file
x Exchanges contents of the holding buffer with the pattern space
y Translates one character to another (cannot use regular expression metacharacters
with y)

Options
-e Allows multiple edits
-f Precedes a sed script filename
-n Suppresses default output

Syntax usually consists of /pattern/action - The slash(/) are delimiters. Pattern of
execution:
Each pattern is sequentially searched until a match is found.
When a match is found, the corresponding action is performed
When the action is complete, the next pattern is selected and step 1 is repeated.
SED automatically outputs the modified record
When all patterns have been exhausted, the next line is read and step 1 is
repeated.

Useful Regular Expressions
blank lines /^$/
an entire line /^.$/
one or more spaces / */
valid URLs /[a-zA-Z][a-zA-Z]*:\/\/[a-z0-9][a-z0-9\.]*.*/
formatted dollar /\$[0-9]*\.[0-9][0-9]/

You might also like