SED Commands
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
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:
Print lines starting from the 1st match of "Jason" until the 4th line:
Print lines starting from the 1st match of "Raj" until the last line:
Print lines starting from the line matching "Raj" until the line matching
"Jane":
Delete lines starting from the 1st match of "Jason" until the 4th line:
Delete lines starting from the 1st match of "Raj" until the last line:
Delete lines starting from the line matching "Raj" until the line matching "Jane":
Delete all comment lines (assuming the comment starts with #):
Replace Manager with Director only on lines that contain the keyword 'Sales':