BOSS-Linux/C2/Simple-filters/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Simple Filters

Author: Balasubramaniam S N

'Keywords: head, tail, sort, cut, paste, grep, sed

Resources for the tutorial: File:Simple filter Linux.zip


Visual Cue
Narration
Display Slide 1


Hello and welcome to the Spoken Tutorial on Simple Filters with Linux.
Display Slide 2


Here we will be learning about head, Tail, sort, cut and paste. All these are command line text manipulation tools.
If you see a #</ symbol on the Terminal you need to become root to execute those commands (sudo su or su root), if you see a $ symbol on the Terminal you can be an ordinary user to execute those commands.
I assume that you did a default installation and did not modify any path were the files are getting saved.
I am using Ubuntu 10.04 for this tutorial. Pre-requisites for this module are the ability to use the mouse , keyboard, maximize and minimize buttons on a window.
Display Slide 3

head command


We use the Head command followed by an ascii file name, to display the first 10 lines of a file by default.
Let us create a file. Go to Applications > Accessories > Text Editor. I already have these numbers in another file for the sake of time constrain . Let me copy and paste.

1

2

22

2

4

6

7

8

99

10

11

12



Hit on File , Save. Name the file as numbers.txt and hit on save . Close this file.
Now go to Applications > Accessories > Terminal.
Type at the command prompt

$ls

numbers.txt

Let us try to see if we can see the file we created. Type ls and hit on enter. What we did here is to list all the folders and files on our home directory.
Type at the command prompt

$cat numbers.txt

Now we can use the cat command to read the contents of the file we created.
Cat num Hit on tab to autofill the file name. Hit on Enter.
Let us do the same with head command.
Type at the command prompt

$head numbers.txt

Head numbers.txt enter
Now the first 10 lines are displayed.
Type at the command prompt

$head -n5 numbers.txt

If we want to see the first 5 lines, use the option -n5 in between the head command and the file.


Hit on Up arrow, -n5 and Enter.


Now the first 5 lines alone are displayed. Let us switch back to the presentation,
Display Slide 4

tail command


The tail command works exactly opposite to the head command, it displays the last 10 lines of a file by default.
I am hitting on ALT+ Tab to switch to the terminal.
Type at the command prompt

$tail numbers.txt

tail numbers.txt
Type at the command prompt

$tail -n5 numbers.txt

If we want to see just the last 5 lines, use the option -n5 in between the tail command and the file name.
-n5 Enter
Switch to slides
A log file contains events which took place in a system.



Auth.log file maintains log's for who logged in & who logged out.
The most useful option of the tail command is to use -f option to follow the tail of a log file. If a new line is appended to the log file, the tail command will take that as the last line and display 10 lines by default above it.
Go to the terminal
Type at the command prompt

$tail -f /var/log/auth.log

tail -f /var/log/auth.log
Let me resize this terminal
Let us open another Terminal , Application- > Accessories - > Terminal.


Let me resize the terminal so that i can show you in a single screen how tail follows the last line in a log file.
Type at the command prompt

$su currently logged in user name></

Try to su as yourself, hit on enter.
example

$su bala

Give in some wrong password hit on enter.
You will see that the terminal on which tail is running is appended with a new log. The date & time specifies when the authentication failure took place.
Type date hit on enter to verify the system date and time.
Type Exit to close this terminal.
Hit CTRL + C to close the running tail command & maximize the screen.
Display Slide 5

Common Linux log files name and usage


We have seen only auth.log file in the previous example.These are the commonly used log files in Linux.
A linux system administrator will look into these log files for more information to troubleshoot his machine if there is a problem.


Display Slide 6

sort command


Sort command as the name suggest will sort a file for us in both ascending and descending order.
Type at the command prompt

$sort numbers.txt

sort numbers .txt .This would sort our numbers.txt file in ascending order.
Notice something is strange in there, sort just looks at the first character to sort, so 10,11&12 appear before the number 2.
Type at the command prompt

$sort -n numbers.txt

To avoid this add the option of -n Enter



Now sort looks at the entire number to sort them.
Type at the command prompt

$sort -rn numbers.txt

To sort number.txt in reverse order add an option of -r.


Type at the command prompt

$sort -run numbers.txt

We have numbers that repeat in this file, to pull out just the unique numbers add another option of -u .
Go to Terminal.
Up Arrow U enter, Previously two 2's were displayed . Now only one 2 is displayed.
Now we will see how we can sort a file based on certain column.
Let us create a file and key in these as shown below. Go to Application > Accessories > Text Editor.
I already have the data in another file for the sake of time constrain let me copy & Paste the value, CTRL+C; CTRL+V.
File, Save it as marks.txt, Hit on Save.
Don't mind the special characters on this file I don't want someone to sue me for giving them low marks. Close this file.
Let us sort based on the second column of marks.txt file.
Go to the terminal.
Type at the command prompt

$sort marks.txt -t " " -k2

$sort space marks.txt space -t space open inverted comma, space close inverted comma, space here -t stands for the delimiter & the space inbetween the quotes represents it. -k2 for the second column on which sort should be performed. Hit on Enter.
Cat marks.txt
This is the original file, if you see Avir is moved above then Bala came below when we sorted based on the second column alone.
Display Slide 7 cut command Cut command is used to cut just certain informations from a file.
Let us pull out the names from marks.txt
Let us go to the terminal ALT+Tab
Type at the command prompt

$cut marks.txt -d " " -f2


$cut space marks.txt space -d space open inverted comma space close inverted comma, space here in cut command -d is the delimiter & the space inside the quotes represents the delimiter, -f2 for the second column. Hit on enter.
Display Slide 8 paste command


Paste command will merge corresponding subsequent lines of the files.
Let us use both the numbers.txt and marks.txt file now.
Type at the command prompt

$paste numbers.txt marks.txt

Go to the terminal.
Paste numbers.txt marks.txt enter
Now the first line of marks.txt was appended to the first line of numbers.txt.
Type at the command prompt

$paste numbers.txt marks.txt > concatefile.txt

$cat concatefile.txt

We can use the redirect key to redirect this output to some other file called concatefile.txt.
Go to the terminal.
Hit Up arrow. Hit on the redirect key which is the Greater than symbol concatfile.txt. Hit on Enter.
Cat concatfile.txt
Let us go to the slide.
Type at the command prompt

$paste -s numbers.txt

If we want paste to print out the numbers serially delimited with tab we can use the -s option.
Paste -s numbers.txt
Go to the slide.
Display Slide 9 Spoken Tutorial project is a part of Talk to a Teacher project, Supported by the National Mission on Education through ICT, Goverment of India.
For more information you can refer to the following link http://spoken-tutorial.org/NMEICT-Intro This is Bala. Thanks for watching this video & don't forget to leave a comment, ba- bye.

Contributors and Content Editors

Nancyvarkey, PoojaMoolya