Useful Unix Functions

Before reviewing these functions, you should be familiar with the concepts of stdin and stdout. In unix stdin is the keyboard, and stdout is the screen. Therefore, when you use std::cin or scanf in C/C++ you are reading from stdin and when you use std::cout or printf you are writing to stdout. In unix, stdin and stdout can be redirected such that they don't represent the keyboard and screen. For example, you can take the stdout of one program and send it to stdin of another.

Functions Used for Redirection

cat

Concatenates files and prints them to the screen. If you use just one file, cat prints that file to the screen. Note the using cat with just one file waste's a process; it's better to use exec.

host $ cat data.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $ cat data.txt data.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $

|, mkfifo, mknod

All of the functions above are ways of creating unix pipes. Data "flows" thought the pipe from the program on the left side of the | to the program on the right side. Pipes send the output of one process to the input of another. You can use pipes together with cat, exec, redirection or any other code that prints data to the screen to send data to various unix or Glotzilla functions.

host $ cat data.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $ cat data.txt | avg    
5.431234  

host $

Note that here, avg is a Glotzilla function that calculates the average.

<<. <, >, >>, etc. (redirection)

You can redirect data written to stdout (the screen) by using the redirect operators.

host $ cat data.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $ cat data.txt > temp.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $ cat temp.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $ cat temp.txt | avg    
5.431234  

host $ avg < temp.txt  #same thing as above
5.431234  

cut / paste

The cut / paste functions can be useful for data processing. Consider the following file containing coordinates data

host $ cat data.txt
8.0234 3.31234 0.34232
1.2343 9.35343 9.89734
...

host $ cat data.txt | cut -f1-2       #cut the first two fields
8.0234 3.31234 
1.2343 9.35343
...

host $ cat data.txt | cut -f1 > temp.txt
host $ paste data.txt temp.txt
8.0234 3.31234 0.34232 8.0234
1.2343 9.35343 9.89734 1.2343 
...

Other Useful Functions

time

The time function can be used to create a quick time profile of a simulation or analysis code:

host $ time ./simulation
            
           real    0m3.734s
           user    0m2.399s
           sys     0m0.067s