sed

sed is a stream editor, it works on any stream of bytes. The input stream can be specified as file after the sed-command or send to if by the pipe’|’ from a previous command. The output is send to std-out (standard out) where normal redirection can be applied.

patterns between (….) can be called again as 1 up to 9. The fist
defined is 1 the ninth 9.

Sed works in pattern-space, line are put in pattern space and modified
there. ^ represents the start of pattern, $ the end. Newline character a not
read into pattern-space.

Sed also has hold-space you can put line in (see below)

sed ‘s/<old_string>/<new_string>/g’ <file>
Substitute, no whitespace is allowed after the command (s), the first
character following s is the fieldseperator./g substitutes all matching patterns in a line

/<x> substitutes only the <x>’th occurrence of the
old_string

/p print after substitute (if sed is called with -n

/w <file> write to file after substitute

sed ‘s/<old_string>/”&”<new_string>/’
“&” is the matched string.
sed ‘y/<string1>/<string2>
Translate, <string1> is as long as <string2>. any
matching character from <string1> is replaced by the resp.
character from <string2>
sed ‘/<pattern>/s/<old_string>/<new_string>/g’
<file>
Substitute in all lines containing <pattern>
sed
‘/<pattern1>,<pattern2>/s/<old_string>/<new_string>/g’
<file>
Substitute in all lines from pattern1 until pattern2 containing. $ is
pattern for EOF
sed ‘1d’
Delete first line
sed -n ‘/<pattern>/p’
Print all lines containing <pattern>, -n means don’t print by
default, /p forces printing the found lines.
sed ‘/<pattern/{
cmd
cmd
}
Execute all cmd’s on the lines matching <pattern>
sed ‘/<pattern>/a <text>’
Add <text> after matching line
sed ‘/<pattern>/i <text>’
Insert <text> before matching line
sed ‘/<pattern>/c <text>’
Replace matching line by <text>
sed ‘/<pattern>/p’
Print matching line
sed ‘/<pattern>/d’
Delete matching line
sed ‘/<pattern>/q’
Stop sed after matching line
sed ‘/<pattern>/n’
Goto next line in input file, you can also to pd (print, delete to
force this)
sed ‘/<pattern>/r <file>’
Read <file> after matching line
sed ‘/<pattern>/w <file>’
Write matching line to <file>
:label
Label, a named place in a program you can jump to
b label
Jump to a label
t label
Jump only if a substitute was succesfull in the current line or after
the last t for the current line.
N
Read next line and glue it to the currentline with a newline
character
P
Write until first newline (all if no newline is found).
D
Delete until first newline. Jump to start of script as if a new line
was read.s/^[^n]*n// has the same effect but does continue the script.

Substitute from the start of pattern space all not-newlines until
the first newline with nothing.

h
Copy pattern-space to hold-space H=h+n
g
Copy hold-space to pattern-space G=g+n
x
Exchange hold-space and patternspace.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.