- Unix / Linux - The vi Editor
- Unix / Linux - Communication
- Unix / Linux - Processes
- Unix / Linux - Pipes & Filters
- Unix / Linux - Basic Utilities
- Unix / Linux - Environment
- Unix / Linux - File Permission
- Unix / Linux - Directories
- Unix / Linux - File Management
- Unix / Linux - Getting Started
- Unix / Linux - Home
Unix / Linux Shell Programming
- Unix / Linux - Manpage Help
- Unix / Linux - Shell Functions
- Unix / Linux - IO Redirections
- Unix / Linux - Quoting Mechanisms
- Unix / Linux - Shell Substitutions
- Unix / Linux - Loop Control
- Unix / Linux - Shell Loops
- Unix / Linux - Decision Making
- Unix / Linux - Basic Operators
- Unix / Linux - Using Arrays
- Unix / Linux - Special Variables
- Unix / Linux - Using Variables
- Unix / Linux - What is Shell?
- Unix / Linux - Shell Scripting
Advanced Unix / Linux
- Unix / Linux - Signals and Traps
- Unix / Linux - System Logging
- Unix / Linux - System Performance
- Unix / Linux - User Administration
- Unix / Linux - File System Basics
- Unix / Linux - Regular Expressions
Unix / Linux Useful Resources
- Unix / Linux - Discussion
- Unix / Linux - Useful Resources
- Unix / Linux - Commands List
- Unix / Linux - System Calls
- Unix / Linux - Builtin Functions
- Unix / Linux - Quick Guide
- Unix / Linux - Useful Commands
- Unix / Linux - Questions & Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Unix / Linux - Pipes and Filters
In this chapter, we will discuss in detail about pipes and filters in Unix. You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe.
To make a pipe, put a vertical bar (|) on the command pne between two commands.
When a program takes its input from another program, it performs some operation on that input, and writes the result to the standard output. It is referred to as a filter.
The grep Command
The grep command searches a file or files for pnes that have a certain pattern. The syntax is −
$grep pattern file(s)
The name "grep" comes from the ed (a Unix pne editor) command g/re/p which means “globally search for a regular expression and print all pnes containing it”.
A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching.
The simplest use of grep is to look for a pattern consisting of a single word. It can be used in a pipe so that only those pnes of the input files containing a given string are sent to the standard output. If you don t give grep a filename to read, it reads its standard input; that s the way all filter programs work −
$ls -l | grep "Aug" -rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02 -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros $
There are various options which you can use along with the grep command −
Sr.No. | Option & Description |
---|---|
1 |
-v Prints all pnes that do not match pattern. |
2 |
-n Prints the matched pne and its pne number. |
3 |
-l Prints only the names of files with matching pnes (letter "l") |
4 |
-c Prints only the count of matching pnes. |
5 |
-i Matches either upper or lowercase. |
Let us now use a regular expression that tells grep to find pnes with "carol", followed by zero or other characters abbreviated in a regular expression as ".*"), then followed by "Aug".−
Here, we are using the -i option to have case insensitive search −
$ls -l | grep -i "carol.*aug" -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros $
The sort Command
The sort command arranges pnes of text alphabetically or numerically. The following example sorts the pnes in the food file −
$sort food Afghani Cuisine Bangkok Wok Big Apple Dep Isle of Java Mandalay Sushi and Sashimi Sweet Tooth Tio Pepe s Peppers $
The sort command arranges pnes of text alphabetically by default. There are many options that control the sorting −
Sr.No. | Description |
---|---|
1 |
-n Sorts numerically (example: 10 will sort after 2), ignores blanks and tabs. |
2 |
-r Reverses the order of sort. |
3 |
-f Sorts upper and lowercase together. |
4 |
+x Ignores first x fields when sorting. |
More than two commands may be pnked up into a pipe. Taking a previous pipe example using grep, we can further sort the files modified in August by the order of size.
The following pipe consists of the commands ls, grep, and sort −
$ls -l | grep "Aug" | sort +4n -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02 $
This pipe sorts all files in your directory modified in August by the order of size, and prints them on the terminal screen. The sort option +4n skips four fields (fields are separated by blanks) then sorts the pnes in numeric order.
The pg and more Commands
A long output can normally be zipped by you on the screen, but if you run text through more or use the pg command as a filter; the display stops once the screen is full of text.
Let s assume that you have a long directory psting. To make it easier to read the sorted psting, pipe the output through more as follows −
$ls -l | grep "Aug" | sort +4n | more -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-r-- 1 john doc 14827 Aug 9 12:40 ch03 . . . -rw-rw-rw- 1 john doc 16867 Aug 6 15:56 ch05 --More--(74%)
The screen will fill up once the screen is full of text consisting of pnes sorted by the order of the file size. At the bottom of the screen is the more prompt, where you can type a command to move through the sorted text.
Once you re done with this screen, you can use any of the commands psted in the discussion of the more program.
Advertisements