# Sed Command Examples

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion.

## Syntax

sed OPTIONS... [SCRIPT] [INPUTFILE...]

## Examples

Here is just a brief list of examples of how to use sed

$sed 's/oldtext/newtext/' filemane.txt

Here the “s” specifies the substitution operation. The “/” are delimiters.

Replacing all the occurrence of the pattern in a line

$sed 's/oldtext/newtext/g' filename.txt

Parenthesize first character of each word

$ echo "This Is My Awesome Title" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

Replacing string on a specific line number

$sed '3 s/oldtext/newtext/' filename.txt

Printing only the replaced lines use the -n option along with the /p print flag to display only the replaced lines.

$sed -n 's/oldtext/newtext/p' filename.txt

Replacing string on a range of lines

$sed '1,3 s/oldtext/newtext/' filename.txt

## Deleting lines from a particular file

Examples:

Delete a particular line

$ sed '5d' filename.txt

Delete a last line

$ sed '$d' filename.txt

Delete from nth to last line

$ sed '12,$d' filename.txt

Delete pattern matching line

$ sed '/pattern/d' filename.txt

Learn more at GNU Sed Manual

----------

----------

© DarknessCode - LinuxSucks