Linux-AWK/C2/Basics-of-awk/English
Title of script: The Awk tool Part 1
Author: Sachin Patil
Keywords: Selection criteria, action, formatted printing, fields, Regular expressions, Variables
|
|
Display Slide 1 | Welcome to this spoken tutorial on the awk command. |
Display Slide 2
Learning Objective |
In this tutorial we will learn,
awk command. |
Display Slide 3
System requirement |
To record this tutorial, I am using
Ubuntu Linux 12.04 OS GNU BASH v. 4.2.24(1) Please note, GNU bash version 4 or above is recommended to practice this tutorial. |
Display Slide 4
Introduction |
The awk command is a very powerful text manipulation tool of Linux.
It is named after its authors, Aho, Weinberger and Kernighan. |
Continue Slide
|
It can perform several functions.
It operates at the field level of a record. So, it can easily access and edit the individual fields of the record. |
Let us see some examples.
For demonstration purpose we use the awkdemo.txt file. Let us see the contents of awkdemo.txt.
| |
awkdemo.txt | This is the content of awkdemo.txt file. |
Open the terminal
ctrl+alt+t
|
Now open the terminal by pressing ctrl+alt+t |
type:
|
Now type:
awk space (opening single quote) (front slash) ‘/Pass (front slash)/(in curly brace) {print} (closing single quote) space awkdemo.txt
|
Type
"awk '/M[ei]*ra*/ {print}' awkdemo.txt" [enter] |
We can also use regular expressions in awk
Mira Meeraa
|
awk supports the extended regular expressions (ERE).
Which means we can match multiple patterns separated by a PIPE. | |
Type
"awk '/civil|electrical/ {print}' awkdemo" [enter] |
Type at the terminal:
|
Display slide 7
|
Lets go back to the slides.
|
Switch to the terminal
|
Note that each word is separated by PIPE in the file awkdemo.txt.
|
Type
“awk -F "|" '/civil|electrical/ {print $0}' awkdemo ” [enter]
|
Lets go back to the terminal.
|
Type
“awk -F"|" '/Pass/ {print $2, $3}' awkdemo” [enter] |
Notice that, names and stream of students are the second and third fields.
|
Though it gives the right result the display is all jagged and unformatted.
| |
“awk -F"|" '/Pass/ {printf "%4d %-25s %-15s \n",
NR,$2,$3 }' awkdemo” [enter]
|
We can provide formatted output by using the C style printf statement.
awk space -F”|” space '/Pass/{printf “%4d %-25s %-15s \n”, NR,$2,$3 }' space awkdemo.txt
|
Display Slide 11
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 12
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 13
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. |
The script has been contributed by Sachin Patil.
This is Ashwini Patil from IIT Bombay signning off. Thank you for joining. |