0% found this document useful (0 votes)
4 views

SED Commands

SED Commands in LINUX

Uploaded by

sakethvarma239
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SED Commands

SED Commands in LINUX

Uploaded by

sakethvarma239
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SED Commands

SED

What is SED?
SED stands for Stream Editor. It is a powerful command-line tool used for parsing
and transforming text in a pipeline or script. Typically used on Unix-like operating
systems (such as Linux or macOS), SED allows you to perform tasks like:

1. Text substitution – replacing one string with another (e.g., replacing "apple"
with "orange").
2. Text deletion – removing lines or parts of lines.
3. Text insertion – adding new text or lines at specific locations.
4. Text transformation – using regular expressions to modify patterns.
SED Command Syntax

Basic SED Syntax:

sed [options] {sed-commands} {input-file}

For all sed examples, we'll be using the following employee.txt file.

$ vi employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
The following example prints every line of employee.txt twice:

$ sed 'p' employee.txt

Print each line once (functionally the same as 'cat employee.txt'):

$ sed -n 'p' employee.txt

Print only the 2nd line:

$ sed -n '2 p' employee.txt

Print from line 1 through line 4:

$ sed -n '1,4 p' employee.txt


Print from line 2 through the last line ($ represents the last line):

$ sed -n '2,$ p' employee.txt

Print only odd numbered lines:

$ sed -n '1~2 p' employee.txt


Pattern Matching

Print lines matching the pattern “Jane”:

$ sed -n '/Jane/ p' employee.txt

Print lines starting from the 1st match of "Jason" until the 4th line:

$ sed -n '/Jason/,4 p' employee.txt

Print lines starting from the 1st match of "Raj" until the last line:

$ sed -n '/Raj/,$ p' employee.txt

Print lines starting from the line matching "Raj" until the line matching
"Jane":

$ sed -n '/Raj/,/Jane/ p' employee.txt


Print the line matching "Jason" and 3 lines immediately after that:

$ sed -n '/Jason/,+3 p' employee.txt


Deleting Lines

Delete only the 2nd line:

$ sed '2 d' employee.txt

Delete from line 1 through 4:

$ sed '1,4 d' employee.txt

Delete from line 2 through the last line:

$ sed '2,$ d' employee.txt

Delete only odd number of lines:

$ sed '1~2 d' employee.txt


Delete lines matching the pattern "Manager":

$ sed '/Manager/ d' employee.txt

Delete lines starting from the 1st match of "Jason" until the 4th line:

$ sed '/Jason/,4 d' employee.txt

Delete lines starting from the 1st match of "Raj" until the last line:

$ sed '/Raj/,$ d' employee.txt

Delete lines starting from the line matching "Raj" until the line matching "Jane":

$ sed '/Raj/,/Jane/ d' employee.txt


Delete all the empty lines from a file:

sed '/^$/ d' employee.txt

Delete all comment lines (assuming the comment starts with #):

sed '/^#/ d' employee.txt


SED Substitute Commands

Replace all occurrences of Manager with Director:

$ sed 's/Manager/Director/' employee.txt

Replace Manager with Director only on lines that contain the keyword 'Sales':

$ sed '/Sales/s/Manager/Director/' employee.txt

Replace the 1st occurrence of lower case a with upper case A:

$ sed 's/a/A/' employee.txt

Replace all occurrences of lower case a with upper case A:

$ sed 's/a/A/g' employee.txt


Replace the 2nd occurrence of lower case a to upper case A:

$ sed 's/a/A/2' employee.txt

You might also like