|
Software
Files Pipelines / Filters
Directory Shell
Networking Reference
|
Pipelines and FiltersGrep ||SortPipelineUnix allows you to connect processes, by letting the standard output of one process feed into the standard input of another process. That mechanism is called a pipe. Connecting simple processes in a pipeline allows you to perform complex tasks without writing complex programs. For example : How could you use head and tail in a pipeline to display lines 25 through 75 of a file? The command %cat file | head -75 | tail -50 would work. The cat command feeds the file into the pipeline. The head command gets the first 75 lines of the file, and passes them down the pipeline to tail. The tail command then filters out all but the last 50 lines of the input it received from head. It is important to note that in the above example, tail never sees the original file, but only sees the part of the file that was passed to it by the head command. It is easy for beginners to confuse the usage of the input/output redirection symbols < and >, with the usage of the pipe. Remember that input/output redirection connects processes with files, while the pipe connects processes with other processes. FiltersFilters can be placed anywhere in a line that enables them to help direct Unix to do what you want it to do.The common characteristic of all Unix filters is that they can read input from standard input, process it in some manner, and list the results in standard output. GrepThe grep utility is one of the most useful filters in UNIX. Grep searches line-by-line for a specified pattern, and outputs any line that matches the pattern. The basic syntax for the grep command is %grep [-options] pattern [file] If the file argument is omitted, grep will read from standard input. It is always best to enclose the pattern within single quotes, to prevent the shell from misinterpreting the command. The grep utility recognizes a variety of patterns, and the pattern specification syntax was taken from the vi editor. Here are some of the characters you can use to build grep expressions:
%grep 'jon' /etc/passwd to search the /etc/passwd file for any lines containing the string "jon".
% grep '^jon' /etc/passwd to see the lines in /etc/passwd that begin with the character string "jon".
SortA program that reads information and sorts it alphabetically. Few flags are available for sort as shown below :
By default, the ls command sorts the files in a directory in a case-sensitive manner.It first lists those files that begin with uppercase letters and then those that begin with lowercase letters.Let assume we have the output of %ls -1F Archives/ InfoWorld/ Mail/ News/ OWL/ bin/ keylime.pie src/ temp/ xyz.cpp To sort filenames alphabetically regardless of case, you can use sort -f , type %ls -1 | sort-f Archives/ bin/ InfoWorld/ Mail/ News/ OWL/ src/ temp/ xyz.cpp Let us see which file is largest in your home directory.The ls -s command indicates the size of each file, in blocks, and sort -n sorts numerically : %ls -s | sort -n total 127 1 Archives/ 1 InfoWorld/ 1 Mail/ 1 News/ 1 OWL/ 1 src/ 1 temp/ 1234 xyz.cpp It would be more convenient if the largest files were listed first in the output. We will use -r flag to reverse the sort order. %ls -s | sort -nr 1234 xyz.cpp 1 temp/ 1 src/ 1 OWL/ 1 News/ 1 Mail/ 1 InfoWorld/ 1 Archives/ total 127
|