Linux/C3/The-sed-command/English

From Script | Spoken-Tutorial
Revision as of 15:56, 14 December 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Sachin p:

review doneTitle of script: sed - The stream editor

Author: Sachin Patil and Anirban

Keywords: stream, editor, display, substitute, insert, delete


Visual Cue
Narration
Display slide 1 Welcome to this spoken tutorial on the sed - the stream editor.
Display slide 2

Learning objectives

In this tutorial we tutorial we will learn usage of sed command.
Display slide 3

System Requirement

To record this tutorial, I am using

Ubuntu Linux 12.04 Operating System and

GNU BASH version 4.2.24

Please note, GNU bash version 4 or above is recommended to practice this tutorial.

Display slide 4

Prerequisites

As prerequisites

You should know Basics of linux terminal

For relevant tutorials please visit our website:

http://spoken-tutorial.org

Display slide 5

Introduction Let us start with an introduction to sed.

sed is one of the most versatile tool in Linux.

Its a stream editor.

sed finds some pattern of text in a particular location of a file.

Then it behave as a display or editing function.

It performs editing like insertion, substitution, deletion of the matched text.

Open the file seddemo.txt


[enter]

Let us first start with some examples.

We will see how to print using the sed command.

I have a file called seddemo.txt in home directory.

Let us view its content.

In this file we have some enteries like roll no, name, stream, marks, pass or fail and the stipend amount.


At the prompt


sed '2p' seddemo.txt [enter]


Hover your mouse on the second line

Now suppose we want to print the second line of the file.

For this we need to oepn the terminal by pressing CTRL + ALT and T keys simultaneously on your keyboard.

Now Type:

sed space (in single quotes) ‘2p’ space seddemo.txt

Press Enter

Here 2 denotes the location which is the second line.

p denotes the action, which is printing(p).

Look at the output.

It shows the entire file but see that the second line is printed twice.

This is the default behaviour of the action p.

At the prompt


sed -n '2p' seddemo.txt


[enter]

To only print the second line

type:

sed space (minus) -n space (in single quotes) 2p space seddemo.txt

Press Enter.

Highlight


-n


2


p


seddemo.txt

We see only the second line is printed.

-n stands for ‘silent mode’ which will suppress all unnecessary output.

Then we give the location in the stream that we want to edit or display.


We want to select the second line.


p indicates the action we want to take ie to print the second line.


And seddemo.txt is the name of the file


This is the general syntax of sed command.



At the prompt


sed -n '$p' seddemo.txt

[enter]


Now let us print the last line


type:

sed space (minus) -n space (in single quotes) (dollar) $p space seddemo.txt


Press Enter.


We see that the last line is printed.

Now come back to the text editor.

At the prompt


sed -n '3,6p' seddemo.txt


[enter]

Suppose we want to print the enteries from 3rd to 6th

For this we need to type on the terminal:

sed space (minus) -n space (in single quotes) ‘3 (comma) ,6p’ space seddemo.txt


Press Enter.

The output is displayed from the third line to the sixth line.

At the prompt


sed -n '3,6!p' seddemo.txt


[enter]

Any of the actions can be reversed by using the exclamation mark before the action.

Say if we had to print all lines except from 3rd to 6th we would type:

sed space (minus) -n space (in single quotes) ‘3 (comma) ,6 (exclamation mark) !p space seddemo.txt


Press Enter.

The output is displayed.

Let us switch back to the slides.

Line addressing and context addressing.

Display slide 6 So far, we specified the lines in the file on which the action needs to be taken.

This is known as line addressing.

Address specified by the line numbers.

This is one way of addressing.

Another way of addressing is Context addressing

Lines that contain particular context say a particular word.

If we want to take actions on lines that contain a particular word we use context addressing.

Regular expressions can be used.

Let us see an example.

Come back to our editor.

At the prompt


sed -n '/[cC]omputers/p' seddemo.txt


[enter]

Say we want to print those lines which have the word computers.

Come back to our temrinal.

type:

sed space (minus) -n space (in single quotes) (front slash)(opening square bracket) (opening square bracket) (front slash) ‘/[cC]omputers/p space seddemo.txt

Press Enter.

We see the lines with the word computers is displayed.

We write pattern within square brackets. This is to match any one or both of the characters within square brackets.

When we need to match patterns the pattern needs to be typed between front slashes.

At the prompt


sed -n '/[cC]omputers/w computer_student' seddemo.txt


[enter]

We can print it in file as well using the w option.

For this type:


sed space (minus) -n space (in single quotes) (front slash) (opening square bracket) (opening square bracket) (front slash) /[cC]omputers/w space computer_student space seddemo.txt

Press Enter.

Now all the matching lines would be transferred to the file computer_student.txt.


At the prompt

cat computer_student


[enter]

Let us view the content of computer_student

type:

cat space computer_student

Press Enter.

At the prompt


sed -n -e '/electronics/w electro’ -e

‘/civil/w civil' seddemo.txt


[enter]


We can also have patterns that we can write to different files.

Type:

sed space (minus) -n space (minus) -e space (in single quotes) (front slash) ‘/electronics/w space electro.txt’ (minus) -e space (in single quotes) (front slash) ‘/civil/w space civil.txt’ space seddemo.txt

Press Enter

-e is used to combine multiple methods

Press Enter

This would create two files electro.txt and civil.txt.

cat electro

[enter]


cat civil

[enter]

To see what they contain type:

cat space electro.txt

This will display the enteries with the word electronics.

Let us see the content of civil file.

Then type:

cat space civil.txt

press Enter

This will display the enteries having the word civil.

We will see some more set of commands in another tutorial.

I will use the same program.

Display slide 7

Summary

Let us summarize,

In this tutorial we learnt,

sed.

To print using sed.

Line Addressing.

Context Addressing.

Assignment As an assignment,

Use the same text file seddemo.txt.

Try to print records from 6th to 12th line.

Display Slide 8

Acknowledgement Slide

Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide 10

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

No Last Slide for tutorials created at IITB

Display the previous slide only and narrate this line.

Contributors and Content Editors

Ashwini