UNIX / Linux Pipes and Filters - Unix/Linux

What are UNIX / Linux Pipes and Filters?

In this episode, we will discuss in factor about pipes and filters in UNIX. You can attach two commands collectively so that the output from one program becomes the input of the next program. Two or more commands associated in this way form a pipe.

To make a pipe, put a vertical bar (|) on the command line between two instructions.

When a program takes its input from another program, it performs some action on that input, and writes the result to the pattern output. It is referred to as a filter.

The grew Command

The grew command search a file or files for lines that have a sure pattern. The syntax is –

The name "grew" comes from the end (a UNIX line editor) command g/re/which means “globally search for a regular expression and print all lines containing it”.

A normal expression is either some plain text (a word, for instance) and/or particular characters used for pattern matching.

The simplest use of grew is to look for a pattern consisting of a particular word. It can be used in a pipe so that only those lines of the input files contain a given string are sent to the pattern output. If you don't give grew a filename to read, it reads its standard input; that's the way all filter programs work –

There are different option which you can use along with the grew command −

S.No. Option & Description
Prints all lines that do not match pattern.
Prints the matched line and its line number.
Prints only the names of files with matching lines (letter "l")
Prints only the count of matching lines.
Matches either upper or lowercase.

Let us now use a normal expression that tells grew to find lines with "carol", followed by zero or other typescript reduced in a regular expression as ".*"), then followed by "Aug".−

Here, we are using the -I selection to have case insensitive exploration –

The sort Command

The sort command arranges lines of text alphabetically or numerically. The following instance sorts the lines in the food file –

The sort commands arrange lines of text alphabetically by defaulting. There are many options that control the arrangement −

S.No. Description
Sorts numerically (example: 10 will sort after 2), ignores blanks and tabs.
Reverses the order of sort.
Sorts upper and lowercase together.
Ignores firstxfields when sorting.

More than two instructions may be linked up into a pipe. Taking a preceding pipe instance using grew, we can further sort the files adapted in August by the order of size.

The following pipe consists of the commands less, grip, and sort

This pipe sorts all records in your directory adapted in August by the order of size, and prints them on the terminal screen. The sort option +4n skip four fields (fields are separated by blanks) then sorts the lines in numeric command.

The pg and more Commands

A long output can generally be zipped by you on the screen, but if you run text during more or use the pg command as a filter; the show stops once the screen is full of text.

Let's suppose that you have a long index listing. To make it easier to study the sorted listing, pipe the output through more as follow –

The screen will fill up once the screen is full of text consisting of lines sorted by the order of the file size. At the bottom of the screen is the more prompt, where you can kind a command to move through the sort text.

Once you're completed with this screen, you can apply any of the commands listed in the discussion of the more programs.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics