Difference between revisions of "Linux/C3/The-sed-command/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with 'Sachin p: review done'''Title of script''': sed - The stream editor '''Author: Sachin Patil''' '''Keywords: stream, editor, display, substitute, insert, delete''' {| style=…')
 
Line 31: Line 31:
 
'''Ubuntu Linux''' 12.04''' Operating System''' and
 
'''Ubuntu Linux''' 12.04''' Operating System''' and
  
'''GNU BASH''' version 4.2.24(1)
+
'''GNU BASH''' version 4.2.24
  
 
Please note,''' GNU bash''' version 4 or above is recommended to practice this tutorial.
 
Please note,''' GNU bash''' version 4 or above is recommended to practice this tutorial.
Line 39: Line 39:
  
 
Prerequisites
 
Prerequisites
| style="border:1pt solid #000000;padding:0.097cm;"| To practice this tutorial,
+
| style="border:1pt solid #000000;padding:0.097cm;"| As prerequisites
  
 
You should know Basics of linux terminal
 
You should know Basics of linux terminal
Line 51: Line 51:
  
 
Introduction
 
Introduction
 +
Let us start with an introduction to sed.
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''sed''' is one of the most versatile tool in Linux.  
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''sed''' is one of the most versatile tool in Linux.  
  
Line 68: Line 69:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Let us first start with some examples.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Let us first start with some examples.
  
Let us see how to print using the '''sed''' command.
+
We will see how to print using the '''sed''' command.
  
We have a file called '''seddemo.txt''' in home directory.
+
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.
  
Let us view its content .
 
  
 
|-
 
|-
Line 82: Line 86:
  
 
Hover your mouse on the second line
 
Hover your mouse on the second line
| style="border:1pt solid #000000;padding:0.097cm;"| Now suppose we want to print the second line of the file '''seddemo.txt'''.  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now suppose we want to print the second line of the file.
  
Type:  
+
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'''
 
'''sed''' space (in single quotes) ‘'''2p’''' space '''seddemo.txt'''
Line 132: Line 139:
  
 
'''seddemo.txt'''
 
'''seddemo.txt'''
 +
 +
We see only the second line is printed.
 +
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''-n''' stands for ‘silent mode’ which will suppress all unnecessary output.
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''-n''' stands for ‘silent mode’ which will suppress all unnecessary output.
  
Line 161: Line 171:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| To print the last line  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us print the last line  
  
  
Line 169: Line 179:
  
  
Press Enter.
+
Press '''Enter'''.
 +
 
 +
 
 +
We see that the last line is printed.
 +
 
 +
Now come back to the text editor.
  
 
|-
 
|-
Line 179: Line 194:
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| To print from 3<sup>rd</sup> to 6<sup>th</sup> line
+
| style="border:1pt solid #000000;padding:0.097cm;"| Suppose we want to print the enteries from 3<sup>rd</sup> to 6<sup>th</sup>
  
type:  
+
For this we need to type on the terminal:  
  
 
'''sed''' space (minus) '''-n''' space (in single quotes) ‘'''3''' (comma) ''',6p’''' space '''seddemo.txt '''
 
'''sed''' space (minus) '''-n''' space (in single quotes) ‘'''3''' (comma) ''',6p’''' space '''seddemo.txt '''
Line 187: Line 202:
  
 
'''Press Enter.'''
 
'''Press Enter.'''
 +
 +
The output is displayed from the third line to the sixth line.
  
 
|-
 
|-
Line 204: Line 221:
  
 
'''Press''' '''Enter.'''
 
'''Press''' '''Enter.'''
 +
 +
The output is displayed.
 +
 +
Let us switch back to the slides.
 +
 +
Line addressing and context addressing.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display slide 6
 
| style="border:1pt solid #000000;padding:0.097cm;"| Display slide 6
| style="border:1pt solid #000000;padding:0.097cm;"| So far, we specified the lines in the file on which the action needs to be taken.  
+
| style="border:1pt solid #000000;padding:0.097cm;"| So far, we specified the lines in the file on which the action needs to be taken.
  
This is one way of addressing called '''Line Addressing'''.  
+
This is known as line addressing.  
  
But if we want to take actions on lines that contain a particular word.  
+
Address specified by the line numbers.
  
For such issues the other way of addressing is '''Context Addressing. '''
+
This is one way of addressing.
  
We can use regular expressions.  
+
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.
 
Let us see an example.
 +
 +
Come back to our editor.
  
 
|-
 
|-
Line 229: Line 260:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Say we want to print those lines which have the word '''computers'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Say we want to print those lines which have the word '''computers'''.
  
 +
Come back to our temrinal.
  
We type:
+
type:
  
'''sed''' space (minus) '''-n''' space (in single quotes) (front slash)(opening angle bracket) (opening angle bracket) (front slash) ‘/'''<nowiki>[</nowiki>cC]omputers/p '''space '''seddemo.txt '''
+
'''sed''' space (minus) '''-n''' space (in single quotes) (front slash)(opening square bracket) (opening square bracket) (front slash) ‘/'''<nowiki>[</nowiki>cC]omputers/p '''space '''seddemo.txt '''
  
 
'''Press''' '''Enter.'''
 
'''Press''' '''Enter.'''
  
The lines where the word computers are displayed.
+
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 slashes.
+
When we need to match patterns the pattern needs to be typed between front slashes.
  
 
|-
 
|-
Line 250: Line 284:
 
| style="border:1pt solid #000000;padding:0.097cm;"| We can print it in file as well using the '''w''' option.  
 
| style="border:1pt solid #000000;padding:0.097cm;"| We can print it in file as well using the '''w''' option.  
  
Type at the prompt:
+
For this type:
  
  
'''sed''' space (minus) '''-n''' space (in single quotes) (front slash) (opening angle bracket) (opening angle bracket) (front slash) '''<nowiki>/[</nowiki>cC]omputers/w''' space '''computer_student''' space '''seddemo.txt '''
+
'''sed''' space (minus) '''-n''' space (in single quotes) (front slash) (opening square bracket) (opening square bracket) (front slash) '''<nowiki>/[</nowiki>cC]omputers/w''' space '''computer_student''' space '''seddemo.txt '''
  
 
'''Press''' '''Enter.'''
 
'''Press''' '''Enter.'''
  
Now all the matching lines would be transferred to the file '''computer_student'''.  
+
Now all the matching lines would be transferred to the file '''computer_student.txt'''.  
 +
 
  
 
|-
 
|-
Line 266: Line 301:
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| To view the content of '''computer_student'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us view the content of '''computer_student'''
  
 
type:
 
type:
Line 291: Line 326:
 
Type:  
 
Type:  
  
'''sed''' space (minus) '''-n''' space (minus) '''-e''' space (in single quotes) (front slash) '''‘/electronics/w''' space '''electro’''' (minus) '''-e''' space (in single quotes) (front slash) '''‘/civil/w''' space '''civil’''' space '''seddemo.txt '''
+
'''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''' '''
 
'''-e''' is used to combine multiple methods''' '''
Line 298: Line 334:
 
'''Press Enter'''
 
'''Press Enter'''
  
This would create two files '''electro''' and '''civil'''.  
+
This would create two files '''electro.txt''' and '''civil.txt'''.  
  
 
|-
 
|-
Line 311: Line 347:
 
| style="border:1pt solid #000000;padding:0.097cm;"| To see what they contain type:
 
| style="border:1pt solid #000000;padding:0.097cm;"| To see what they contain type:
  
'''cat '''space''' electro'''  
+
'''cat '''space''' electro.txt'''  
  
'''Press Enter'''
+
This will display the enteries with the word electronics.
 +
 
 +
Let us see the content of civil file.
  
 
Then type:
 
Then type:
  
'''cat '''space''' civil'''
+
'''cat '''space''' civil.txt'''
 +
 
 +
''' press Enter'''
  
And''' press Enter'''
+
This will display the enteries having the word civil.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border:1pt solid #000000;padding:0.097cm;"| Now we will see some more set of commands in another tutorial.
+
| style="border:1pt solid #000000;padding:0.097cm;"| We will see some more set of commands in another tutorial.
  
I will use the same program file.
+
I will use the same program.
  
 
|-
 
|-

Revision as of 15:10, 14 December 2013

Sachin p:

review doneTitle of script: sed - The stream editor

Author: Sachin Patil

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