Difference between revisions of "Linux-AWK/C2/Built-in-Functions-in-awk/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 158: Line 158:
 
|-
 
|-
 
| 03:53
 
| 03:53
| If the number if negative, the '''function '''returns '''nan'''.
+
| If the number is negative, the '''function '''returns '''nan'''.
  
 
|-
 
|-

Revision as of 12:34, 4 July 2019

Time
Narration
00:01 Hello and Welcome to this spoken tutorial on built-in functions in awk.
00:07 In this tutorial we will learn about different types of built-in functions like-

Arithmetic functions

00:15 String functions
00:17 Input/Output functions and Time-stamp functions
00:23 We will do this through some examples.
00:26 To record this tutorial, I am using Ubuntu Linux 16.04 operating system and

gedit text editor 3.20.1

00:38 You can use any text editor of your choice.
00:42 To practice this tutorial, you should have gone through the earlier awk tutorials on this website.
00:49 You should have some knowledge of any programming language like C or C++.
00:56 If not, then please go through the corresponding tutorials on our website.
01:02 The files used in this tutorial are available in the Code Files link on this tutorial page.

Please download and extract them.

01:12 Built-in functions are always available for awk to call.
01:17 First we will learn about the arithmetic functions.

square root function (sqrt (x)) returns positive square root of a number x

01:27 int function truncates x to an integer value
01:32 exponential function gives the exponential of x
01:37 log function returns natural logarithm value of x
01:43 sin and cos gives sine(x) and cosine(x) respectively
01:49 Please note that argument x should be mentioned in radians.
01:55 Let’s look at an example to understand these functions.
02:00 I have already written the code in a file arithmetic underscore function dot awk

The same is available in the Code Files link.

02:10 Here, we are printing the square root of a positive and negative number respectively.
02:17 Next we are printing the integer value for a positive and negative number respectively.
02:24 Then we are printing exponential of a small number and a very large number.
02:31 After that, natural logarithm of positive and negative numbers are printed.
02:38 We are also printing sine and cosine values of 0.52 radian, that is actually 30 degree.

Let us execute the file in the terminal.

02:50 Open the terminal by pressing Ctrl, Alt and T Keys.
02:55 Next go to the folder where you have downloaded and extracted the file using cd command.
03:03 Now type awk space -f space arithmetic_function.awk

And press Enter to see the output.

03:14 Couple of things are clear from this output.
03:18 sqrt() function gives square root of a positive number.
03:23 It returns nan or not a number if the number is negative.
03:29 int() gives the truncated integer of any positive or negative number.
03:36 exp() gives exponential of a number.

If the number is very large, the function will return inf.

03:47 Natural logarithm of positive number is given by log() function.
03:53 If the number is negative, the function returns nan.
03:58 Sine and cosine functions return corresponding values.

You can verify the value using your calculator.

04:07 Now, let us look at random functions.
04:11 rand() returns any random number between 0 and 1. But never returns 0 or 1.
04:21 Generated numbers will be random within one awk execution.
04:27 But predictable across different executions of the awk program.
04:33 srand(x) is used to provide seed value x for random function.
04:39 In absence of x, date and time of day is used as the seed value.

Let us understand these with an example.

04:49 I have written a code for the random function and saved it as random.awk
04:56 Here, inside the for loop, rand() function will generate a random number between 0 and 1.
05:04 Then the generated number will be multiplied by 50 and get printed.
05:10 So, this code will generate 5 random numbers within 50.
05:16 Switch to the terminal and execute the file.

Let me clear the terminal.

05:23 Type: awk space hyphen f space random dot awk and press Enter.
05:31 See, it is giving 5 random numbers.
05:35 What happens if I execute the code again?
05:39 Press the Up arrow key to get the previously executed command and press Enter.
05:47 We are getting the same output. Which means, awk is generating the same set of random numbers for every execution of the script.
05:57 Then how can we get a new set of random numbers in every execution?

Switch to the code once again.

06:06 Before the for loop, type srand() function
06:11 Press Crtl and S keys to the save the file.
06:16 Now switch to the terminal.
06:19 Press the Up arrow key to get the previously executed command and press Enter.
06:27 It is giving a different set of random numbers.
06:31 So, we can generate a new set of random numbers using srand function, when it’s used without an argument.
06:40 Next will see some string functions.

length function gives the length of a particular string s

06:49 index function determines the position of string s2 within the larger string s1.
06:57 For example, index within parentheses within double quotes linux comma within double quotes n, returns 3.

Let us see an example.

07:10 Open the file awkdemo.txt
07:14 We know that each student in the awkdemo.txt file has a 4 digit roll number.
07:21 Due to typing error, the roll numbers may have wrong number of digits.

We can easily detect these using awk commands.

07:30 Switch to the terminal .

Let me clear the terminal.

07:36 Now type the command as shown here.

Here we are checking the length of the 1st field is equal to 4 or not.

07:46 If not, then that particular record will get printed.

Press Enter.

07:53 See, there is one roll-number S02 that has been typed incorrectly.
08:00 It has three digits, whereas all others have four digits.
08:07 The substr(s,a,b) function extracts a substring from a larger string s.
08:14 Let me explain the parameters.
08:17 Here s is the string
08:20 a denotes the position in s from which the extraction would start
08:26 b denotes the number of characters that would be extracted.

Let us see one example.

08:33 Switch to the awkdemo.txt file.
08:37 The first letter of the roll numbers represents the Hostel code where the particular student resides.
08:46 Say we want to find the list of students, who are staying in Hostel A.
08:52 To get that, let’s switch to the terminal.
08:56 Type the command as shown here.
09:00 Here we take the string denoted by $1.
09:05 As we know $1 represents the first field, that is roll number in our case.
09:12 Next, we extract a substring that starts at position one with the character length one.
09:19 Then, if it is equal to capital A, then that line from the file will get printed.

Press Enter to see the output.

09:29 We got the list of students who are in Hostel A.
09:34 We have seen the function split earlier.

So, I am not explaining the details here.

09:40 Please refer to the earlier awk tutorials if you have any doubt.
09:45 There are some other functions which are related to Input/Output.

system() function - helps us to run any unix command within awk.

09:56 Now, we will run the unix command date through awk command.
10:01 In the terminal type the command as shown here.

And press Enter.

10:09 Today’s date and time is displayed on the terminal as an output.
10:15 Now, why do we need this? We have kept only the BEGIN section of the awk command.
10:21 In real world scenarios, we may want to print the system date, before displaying the required output.
10:28 In that case, we would need to execute system commands from awk command.
10:34 There are some functions dealing with time stamps like

systime(), strftime() , etc.

10:43 Browse through the Internet to know about these functions.
10:48 This brings us to the end of this tutorial.

Let us summarize.

10:53 In this tutorial we learnt about different types of built-in functions like

Arithmetic functions, String functions, Input/Output functions and Time stamps functions

11:06 As an assignment-Write an awk program to print the last field of every record
11:13 where name of the student has small u as the third letter using the awkdemo.txt file.
11:22 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

11:30 The Spoken Tutorial Project team conducts workshops using spoken tutorials.

And gives certificates on passing online tests.

For more details, please write to us.

11:43 Please post your timed queries in this forum.
11:47 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

11:59 The script has been contributed by Antara. And this is Praveen from IIT Bombay signing off.

Thank you for joining

Contributors and Content Editors

PoojaMoolya, Sandhya.np14