MTC logo MTC logo Midlands Technical College
Prospective StudentsEnrolled StudentsFaculty & StaffCommunity Partners
About
Academics
Continuing Education
Distance Learning
Student Forms
Library
Class Schedule
News & Updates
Calendar
Directory
Site Map
Ask MTC
MTC Jobs Bookstore
Home
navigation
 
Search

 

About MTC Academics Continuing Education Distance Learning Student Forms Library Class Schedule News & Updates Calendar Directory Site Map Ask MTC MTC Jobs Bookstore Home


CPT 247


Quick Reference

Exercise

Shellscript1

 

What is sed?

The streamlined editor (sed) is a non-interactive editor. It allows one to perform the same kind of editing tasks used in the vi editor.

Instead of working interactively with the editor, the sed progam lets you type the editing commands at the command line, name the file, and then see the output on the screen.

However, the sed editor is non-destructive - it does not change your file unless you save the output with shell redirection. The lines are displayed on the screen by default.

The sed editor processes a file one line at a time and direct its output to the screen; it stores the line it is currently processing in a temporary buffer named a pattern space. After the line is processed and displayed to the screen, it is removed from the pattern space and the next line is read into the pattern space. The original file is not altered or destroyed.

You can use addressing to decide which lines you want to edit. The addresses can be in the form of numbers or regular expressions, or combination of both. Without specifying an address, sed processes all lines of the input file.

Editor sed commands tell sed what to do with the line: print it, remove it, change it, etc.

Format :  sed 'command' filename(s)

Example:  sed '1,3p' newfile

                sed on '/[Jjane/p'  oldfile

                sed '1, 4d' fileabc

Explanation:  1. Lines 1 throough 3 of newfile are printed

                    2. Only lines matching the pattern Jane or jane in

                        oldfile are printed.

                    3. Lines 1 through 4 will be deleted from fileabc.

More examples.

sed '/Thom/d' file    // sed deletes all lines contain in Thom

sed '/Thom/!d' file   //sed deletes lines not containing Thom

sed  '$d' datafile   // delete last line

sed '/north/d' datafile   // all lines containing north are deleted

sed 's/west/north/g' datafile  //substitute north for west globally

sed '5q' datafile   // quit after printing 5 lines