PCPerformanceTools.com
Unix Join Commands

 
  
Article: Unix Join Commands home
 

Read about common Unix Join Commands
Unix Join Commands
Fix PC Errors Automatically with REGCURE!


UNIX is a very versatile operating system, offering any myriad commands for problem solving. It uses its commands as building blocks to create new complex commands.
If you cannot find a single UNIX command to solve your problem, look for an appropriate combination of commands.
The chief tools for joining two separate commands are the pipe, redirection and tee.

Redirection

Redirection changes the assignments for standard input and standard output. It has three operators, >, >> and <.
The > operator makes the filename following it the standard output and the < operator makes the filename following it the new standard input.
Consider the following example.

$ cat file1 > newfile

Now the > operator will guide the output of the cat command towards the new standard output, newfile.
Hence, the string of bytes produced by cat is sent to the file newfile instead of the screen.

If the file does not exist, it is recreated. An already existing file is overwritten. Redirection reassigns the files used as standard output and input. The UNIX operating system treats the terminal or other devices like another file only. Hence, redirection connects programs to files.
This reallocation of standard output and input is temporary. When the command ends the terminal screen once again becomes the standard output.

$ cat < testfile
This is a UNIX test file
$

The < operator will make the testfile the new standard input and cat will display its data.

The append operator >> is similar to >. It also redirects the standard output but if the target file exists, the data is appended to it. An existing file is not overwritten.

$ date >> datefile
This command appends the current date to the end of the datefile.

Pipes

The UNIX pipe facility connects two programs. A pipe (|) makes the output of one program the input of another.

$ grep maths periods | sort
The first part of this command (grep maths periods) will search and output all lines in file periods having occurrences of maths.
The second part (sort) will take as input all lines of file periods containing maths and sort them on the first character. This will be the output.

Consider this example of a command using both redirection and pipe.

$ grep maths periods | sort – subjects > newfile
The hyphen here (-) is recognized as the standard input.
Therefore, in this compound command we have the sorted lines of file period containing maths. The command sort opens the file subjects. Therefore, the content of this file is sorted along with lines of file period containing maths. The output is redirected to a file newfile.

Tee

The tee command reads the standard input and sends it to the standard output. It also sends a copy to a user specified file.

$ cat file1 file2 | tee totalfile |lpr

The contents of files file1 and file2 are combined and stored in a new file total file.
A copy is also sent to the printer.

This can also be done with the help of two commands

$ cat file1 file2 > totalfile
$ lpr totalfile

The tee command will overwrite an existing file. To append use –a option

$ cat file1 file2 | tee –a totalfile |lpr
 

Recommended: Run a free scan of RegCure right now to reveal, isolate, and repair PC errors! Learn more

All Rights Reserved Copyright 2005, 2006  PCPerformanceTools.com Unix Join Commands