Unix Tutorial


What is Unix?

Getting started

Hardware

Software

Basic Commands

vi Commands

 

Files

File Commands

Different types of file

File Redirection

Permissions

Pipelines / Filters

 

Directory

Directory Structure

Directory Command

Shell

Different types of shell

Compiling Program

Networking

Commands

Reference

Reference Commands

online help : Manpages

Useful links/Books

 

Search Engine

Pipelines and Filters


Grep ||Sort


Pipeline

Unix 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.


Filters

Filters 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.

Grep

The 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:

  1. The carat (^) matches the beginning of a line.
  2. The dollar sign ($) matches the end of a line.
  3. The period (.) matches any single character.
  4. The asterisk (*) matches zero or more occurrences of the previous character.
  5. The expression [a-b] matches any characters that are lexically between a and b.
  • Type the command

%grep 'jon' /etc/passwd

to search the /etc/passwd file for any lines containing the string "jon".

  • Type the command

% grep '^jon' /etc/passwd

to see the lines in /etc/passwd that begin with the character string "jon".

  • List all the files in the /tmp directory owned by the user root.
  • The command

    % ls -l /tmp | grep 'root'

    would show all processes with the word "root" somewhere in the line. That doesn't necessarily mean that all the process would be owned by root, but using the grep filter can cut the down the number of processes you will have to look at.

top of the page


Sort

A program that reads information and sorts it alphabetically. Few flags are available for sort as shown below :

Flag
Function
-b Ignore leading blanks
-d Sort in dictionary order(only letters, digits, and blanks are signficant)
-f

Fold uppercase into lowercase; that is, ignore the case of words.

-n Sort in numerical order
-r Reverse order of the sort

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

top of the page