UNIX & GNU/Linux - awk - Read a file and computing the average of students' marks

The awk user command is made to read a file and check each column, one by one. We need to compute the average of students’ marks and to display the total of their average. Let’s go. Explanation In the BEGIN pattern we specify the separator with the FS builtin variable. In our case, it will be the default one: " " (the space character). In the dev pattern, we are going to retrieve marks of each student and make an average of them. ...

October 27, 2012

UNIX & GNU/Linux - awk - Using getline to retrieve data

We’ve already played with awk, passing it a file, but we’re going now to see how to retrieve data from the user with the command line. We would like to know what’s the name of the user. Let’s ask him. Explanation The -f flag is normally to tell awk that we’re going to pass a file as argument. But if we don’t, the shell will wait for a file. Instead of a file, we will give to awk a string that we’ll retrieve thanks to the getline function. ...

October 27, 2012

UNIX & GNU/Linux - awk - Using matching expressions and operators to find a pattern

The user command awk enables to check if an expression could be found in a file. It’s a kind of regex, with classic operators, such as: **? . ***. Let’s do it. Explanation We would like to find every line where there is an “e” followed by any character and a “4”. For example, the following line matches the pattern.: awdawekoodk d ekokd 93239 93994543z k To find it, we have to use ($0 ~/e.4/) because we tell that we want that in each column of the file ($0), we would like to find the pattern “e.*4”. For that we use the tilde (~) and slashes (/*) at the beginning and at the end of our pattern. ...

October 27, 2012

UNIX & GNU/Linux - awk - Using the FILENAME variable

With the awk FILENAME builtin variable, we are able to retrieve the name of the file passed as argument during the execution. It will avoid us to use a regex command line. Sometimes interesting. Let’s see this. Explanation In the example below, we’re going to retrieve the name of the file used as argument and display it. But only if we are scanning the first line. For that, we’re going to use the FNR variable, also a awk builtin variable, to check where we are. If we’re in the first line, we will display the file name. ...

October 27, 2012

UNIX & GNU/Linux - awk - Using NR to display line numbers before each line from a file

We have a file with customers inside. We would like to display all the file with line numbers before each line. Let’s see this in this awk tutorial. Explanation The NR variable is a awk builtin variable, to display the number (a sort of static variable). The $0 represents every column separated by default by the space character. The coma between will create a space character for our output. All the code inside the dev statement, will be executed until there is no line. ...

October 26, 2012

UNIX & GNU/Linux - bash - Setting the colour prompt with PS1

You like your shell but you think that some colours would be better. Let’s see how to set colours of the bash prompt with the PS1 variable. Notice that this tutorial has been made with Ubuntu 12. Some syntaxes may change depending on the operating system. First of all, at your home directory, that you can have with the command “~/”, there is a configuration file named: .bashrc. If you don’t see it, type this: ...

July 9, 2012

UNIX & GNU/Linux - gcc - Option -I for adding includes to the compilation

During the compilation step, we can add includes that will be added to the .o generated. For that, we use the -I option (this is an uppercase “i”, standing for “include”) with the path to the includes we want to add. Let’s suppose we want to add personal includes previously added in a directory. So it can be something like that: gcc -c -o main.o main.c -I"/home/soft/library/personal/include" With a Makefile, we can use this example: ...

June 4, 2012

UNIX & GNU/Linux - gcc - Option -L for adding libraries during linking

We can add libraries during the linking step. This tutorial is the same for g++ of course. We use for that the -L option and add just after (without any space) the path of the libraries. Libraries are generally prefixed with “lib” and suffixed with the .a extension. Let’s suppose we previously created a library named libgreatone.a in /soft/personal/lib. We have so: /soft/personal/lib/libgreatone.a. OK, to call this lib, we have first to specify the path and then the name of this library witout the prefix “lib”. For the first step, let’s use the -L option and for the second one use the -l (an “L” lowercase). Notice that there is no space after the -L nor the -l option. ...

June 4, 2012

UNIX & GNU/Linux - System calls - Difference between fork() and vfork()

The fork() and vfork() syscalls are different. The fork() syscall generates two identical processes with separate memory. The vfork() syscall generates two processes that share the same memory. With vfork() the parent will wait for the child terminates. The parent inherites from the variables that the program is sharing. So after the child was called, all variables modified inside the child will still be modified inside the parent. Let’s see an example with this tutorial of both fork() and vfork() syscalls. ...

May 1, 2012

UNIX & GNU/Linux - System calls - Using wait()

The wait() syscall is often used with the fork() syscall. Indeed, with the wait() syscall we can be sure that the child will be executed before the parent process. In a trivial vision, this is like mutexes for threads. It means that without the following line, the parent and the child will be executed at the same time: wait(&status); If you want to try it, change the line: while (i < 10) by this one on both parent and child loops: ...

May 1, 2012