Sed-Stream-Editor/C3/Advanced-Sed-Command/English
VISUAL CUE | NARRATION |
Slide 1: | Welcome to the spoken tutorial on Advanced Sed Command. |
Slide 2:
Learning Objectives |
In this tutorial,we will learn how sed commands can be used by
|
Slide 3:
System requirements |
This tutorial is recorded using
However, you can use any other editor of your choice. |
Slide 4:
Prerequisites |
To follow this tutorial, you should know basics of
If not, for relevant tutorials please visit our website. |
Slide 5:
Code files |
|
Open the terminal | Open the terminal by pressingCtrl+Alt+T keys simultaneously.
Please remember to press the Enter key after typing every command. |
First we will see how the sed commands are useful with C source code | |
Go to Downloads folder
Open the two c Programs in gedit text editor Scroll down and show the content of the file and highlight the functions |
In the Downloads folder, I have saved two C programs inside the C_Files folder.
Let us open these program files in the text editor. I’ll show how to print only the function names in the program using sed command. Close the file. |
Switch back to the terminal. | |
> cd Downloads/C_Files
find -name "*.c" -print -exec sed -n '/(.*);/p' {} \; highlight find -name "*.c" |
Go to the folder where you have saved the C_Files directory.
Type the command as shown. This find command will search for all filenames with .c extension |
Highlight /(.*);/ |
Then it will find all the function names used in these C programs inside the C_Files folder.
It will match the open and close brackets with any string parameter. It will also match the semicolons at the end which indicates the end of the statement. |
Show the output | The output prints all the function names from both the C program files. |
> cat Primenumber.c
Highlight n1 and n2 |
Next, let us see the source code of Primenumber.c using the cat command.
What if we wish to change the variable name in the source code? For example- Let us change the variable n1 to number1 and n2 to number2 in this program. |
sed -e 's/n1/number1/g' -e 's/n2/number2/g' Primenumber.c | Type the command as shown. |
Highlight number1, number2 in the output
(int number1, number2…) |
Observe the output.
With a single command, the programmers can make changes in the function name or variable name. |
Python source code | Next let’s see how sed commands are used with Python program files. |
Go to Downloads folder and point to Python_ProjectFiles
Open the folder and show the files. |
I have saved a sample Python_ProjectFiles directory inside the Downloads folder.
It has three Python program files. |
Open the three files and arrange it in the same window
Press CTRL +F and type ConnectionDatabase and highlight ConnectionDatabase |
Let us see the source code of these files.
First I will search for the word “ConnectionDatabase” in all these files. I wish to replace “DatabaseConnection” instead of “ConnectionDatabase” in all occurrences. Let us see how to do this using sed. |
Type cd .. >cd Python_ProjectFile |
Switch back to terminal.
Go to the folder where the Python programs are saved on your machine. |
sed -n '/ConnectionDatabase/p' *.py | Type the command as shown to search ConnectionDatabase in all the Python files. |
Highlight the output | The above command will show the lines where 'ConnectionDatabase' is present.
But it does not show the filename. |
find -name "*.py" -print -exec sed -n '/ConnectionDatabase/p' {} \;
Highlight find and sed Highlight find -name "*.py" -print -exec Highlight sed -n '/ConnectionDatabase/p' {} \; |
Now type this command on the terminal.
This will fetchthe filename followed by the matched patternwithin that file. Here we have combined the find unix command and sed command. This find commandwill find all the filenames with .py extension. Then it will print the filenames on the terminal and provide the input to the sed command. Finally the sed command processes the file based on the pattern we specified. |
Highlight the output | Now we see the information about the filename and matched pattern in that file. |
Next, we will search "ConnectionDatabase" and replace it with "DatabaseConnection". | |
find -name "*.py" -print -exec sed -n 's/ConnectionDatabase/DatabaseConnection/gp' {} \; | For that, type the command as shown.
We have successfully replaced the word ConnectionDatabase with DatabaseConnection. |
Highlight the output in the terminal window | However, the output has not been saved into any file.
It is just displayed on the terminal. |
find -name "*.py" -print -exec sed -i's/ConnectionDatabase/DatabaseConnection/g' {} \; | To make changes in the original file, type the same command with -i, as shown.
This command will make the replacement changes in the Python files. Let us cross-check the changes in the files. |
Downloads>> go to the folder Python_ProjectFile
Open the file with .py extension >> highlight the replaced string |
Go to the folder Python_ProjectFiles.
Open the Python source code in the text editor. We can see that we have successfully replaced the string in the .py files. This feature is very helpful for programmers to replace certain code in the entire project file. |
Bash | Next we will see how sed commands are used in Bash shell scripts. |
Slide 6:
Linux - System Administrators |
Let us see an example of how System Administrators manage the configuration files on the server.
System Administrators mostly work with configuration files for multiple sites. |
Slide 7:
Example |
As an example, we will learn to create a separate apache config file for a list of websites.
For this, we need to have two files
We can do this task using only a single one line command. Or we can do this by copying a file and changing the website name at each place in the config file. |
Let us learn how to do this task. | |
Go to the Downloads folder.
Locate BASH_Example Point to 000-default.conf and website_list. open 000-default.conf in gedit open websitelist in gedit Point to sitefiles folder |
Go to the Downloads folder.
Here I have saved a BASH_Example directory. Inside this, I have saved two text files named 000-default.conf and websitelist. Let us open 000-default.conf. This is the template of apache config file. websitelist contains the list of site names. We have 3 website names here. There is another empty folder named as sitefiles where the output will get stored. Here a separate config file will be created for each website with the necessary changes in the file. |
Type
>cd .. > cd Bash_Example |
Let us see the command to do this.
Switch back to the terminal. Go to the folder Bash_Example where the required files are saved. |
for i in $(cat websitelist); do sed "s/localhost/$i/" 000-default.conf > sitefiles/$i.conf; done
Highlight for i in $(cat websitelist)
Highlight sed "s/localhost/$i/" 000-default.conf
Highlight > sitefiles/$i.conf; done
|
Type the for loop command as shown and press Enter.
In this Bash script, the for loop is used for fetching all the site names from the websitelist file. Next we are replacing it with localhost in the file 000-default.conf Then we are storing the output of the sed command in a separate file within sitefiles folder. Each file is saved with a specific site name and .conf extension. |
Go to the sitefiles folder.
Point to the three files. Open a file and highlight |
Let us now check the output.
Go to the sitefiles folder. We can see three separate config files, as expected. Open each file and check the replacement in the place of localhost. |
Understanding sed commands will surely make the System Administrator's daily task easy. | |
Slide 9:
Summary |
With this we come to the end of this tutorial. Let us summarize. |
In this tutorial, we learnt how sed commands can be used by
| |
Slide 8:
Assignment: |
As an assignment do the following
|
Slide 10:
(About Spoken Tutorial Project) |
The video at the following link, summarizes the Spoken Tutorial project.
Please download and watch it. |
Slide 11:
(About Spoken Tutorial Project) |
The Spoken Tutorial Project Team conducts workshops and gives certificates.
For more details, please write to us. |
Slide 12:
Forum questions: |
Please post your timed queries on this forum. |
Slide 13: Acknowledgement | Spoken Tutorial project is funded by the Ministry of Education (MoE), Govt. of India. |
This is Pooja from Spoken Tutorial project signing off.
Thanks for joining. |