436 puntos de vista

Pipes and Filters in Linux/Unix

Pipes and Filters in Linux/Unix

Pipes in UNIX

The novel idea of Pipes was introduced by M.D Mcllory in June 1972– version 2, 10 UNIX installations. Piping is used to give the output of one command (written on LHS) as input to another command (written on RHS). Commands are piped together using vertical bar “ | ” symbol.



  • Input: ls|more
  • Output: more command takes input from ls command and appends it to the standard output. It displays as many files that fit on the screen and highlighted more at the bottom of the screen. To see the next screen hit enter or space bar to move one line at a time or one screen at a time respectively.

Filters in UNIX

In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin, perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can be managed as per preferences using redirection and pipes. Common filter commands are: grep, more, sort.

1. grep Command:It is a pattern or expression matching command. It searches for a pattern or regular expression that matches in files or directories and then prints found matches.



The Options in grep command are:

Grep command can also be used with meta-characters:


* is a meta-character and returns matching 0 or more preceding characters

2. sort Command: It is a data manipulation command that sorts or merges lines in a file by specified fields. In other words it sorts lines of text alphabetically or numerically, default sorting is alphabetical.


The options include:


3. more Command: It is used to customize the displaying contents of file. It displays the text file contents on the terminal with paging controls. Following key controls are used:

  • To display next line, press the enter key
  • To bring up next screen, press spacebar
  • To move to the next file, press n
  • To quit, press q.



While using more command, the bottom of the screen contains more prompt where commands are entered to move through the text.

pr command

pr command helps in generating the input in a printable format, with a properly defined column structure.

Syntax of pr command:

Sample code:

Let’s say we created a file abc.txt that lists numbers from 1-to 10.


Now, let us apply a filter on the above file using the pr command :


Explanation :

By using the pr command in the above file, we filtered the contents of file.txt into 3 columns.

Common Unix filter programs are: cat, cut, grep, head, sort, uniq, and tail. Programs like awk and sed can be used to build quite complex filters because they are fully programmable.

What is filter in Unix with example?

In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin, perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can be managed as per preferences using redirection and pipes. Common filter commands are: grep, more, sort.

What is filter command?

Filters are commands that always read their input from ‘stdin’ and write their output to ‘stdout’. Users can use file redirection and ‘pipes’ to setup ‘stdin’ and ‘stdout’ as per their need. Pipes are used to direct the ‘stdout’ stream of one command to the ‘stdin’ stream of the next command.

What is the default function of head filter in Unix?

It is the complementary of Tail command. The head command, as the name implies, print the top N number of data of the given input. By default, it prints the first 10 lines of the specified files.

Is awk a filter in Unix?

Awk is a scripting language used for manipulating data and generating reports. Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then performs the associated actions.

How do I redirect in Unix?

Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character How do I get the first 10 lines in Linux?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press . By default, head shows you the first 10 lines of a file. You can change this by typing head -number filename, where number is the number of lines you want to see.

How do you use head commands?

How to Use the Head Command

  1. Enter the head command, followed by the file of which you’d like to view: head /var/log/auth.log.
  2. To change the number of lines displayed, use the -n option: head -n 50 /var/log/auth.log.

How to use sed command in Unix with examples?

SED command to delete the First line from the file, 2. SED command to delete the Last line from the file 3. SED command to delete the lines from 1,3 in a file 4. SED command to remove blank lines from a file Here “^” symbol represents the starting point of a line and “$” represents end of the line. Whereas “^$” represents the empty lines. 5.

How is SED used to delete a file?

Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file. Deleting lines from a particular file : SED command can also be used for deleting lines from a particular file. SED command is used for performing deletion operation without even opening the file

Tim Dennis

After attending a bash class I taught for Software Carpentry, a student contacted me having troubles working with a large data file in R. She wanted to filter out rows based on some condition in two columns. An easy task in R, but because of the size of the file and R objects being memory bound, reading the whole file in was too much for my student’s computer to handle. She sent me the below sample file and how she wanted to filter it. I chose AWK because it is designed for this type of task. It parses data line-by-line and doesn’t need to read the whole file into memory to process it. Further, if we wanted to speed up our AWK even more, we can investigate AWK ports, such as MAWK, that are built for speed.

Let’s look at the data we want to filter

What we want to do is get the rows from Chr (column 7) when it equals 6 and also the Pos (column 8) when the values are between 11000000 and 25000000.

Let’s start working out parts of the code in AWK. If you aren’t familiar with AWK, it’s a programming language designed for text processing and data extraction. One of the things it does well is recognize fields in the data. For instance, we know we have 8 columns delimited by tabs in our data, but if you didn’t know how many columns you have, you can find this out with a bit of AWK:

NF is an AWK built in variable and it stands for number of fields. We pipe this to uniq because the default behavior will print the number of columns for each row and since each row has the same number of columns, uniq will reduce this to one number.

Printing Fields and Searching

We can also use AWK to select and print parts of the file. Let’s do this now.

Notice that there’s no formatting of the output. There are many ways to format and structure the output in AWK. Checkout the printing section on the AWK user guide for more information on this.

Now we’ve selected a couple of columns to print out, let’s use AWK to search for a specific thing – a number we know exists in the dataset. Note that if you specify what fields to print out, AWK will print the whole line that matches the search by default.

Instead of matching a unique number, we could have matched on a string pattern or regular expression. In that case, AWK would return every line that matches the pattern. In our case above, that number occurs once in the data file, but we could have used a regular expression or a range pattern instead. For more on finding patterns in AWK, check out the Patterns, Actions and Variables section of the AWK guide.

Filtering Rows Based on Field Values

Ok, now we know how to access fields (columns) and find patterns in our document, but how can we control what we want to search on and where? Our initial problem requires that we look into the Chr field to get only lines with the value 6. Then we want to look into the Pos field to grab the lines where those values are between 11000000 and 25000000. To do this in AWK, we need to use the if control statement along with a conditional expression. Let’s run one now and explain after:

Above, we use the keyword if and then a conditional expression, ($7 == 6) , based on the column variable, $7 we want to test on. The dollar sign here denotes we are working with a variable and in this case, AWK knows that $7 means the 7th field in our dataset. Likewise, $6 would mean the 6th field and so on. == is often used in programming languages to test for equality because a single = is often used for object assignment. What we are saying here is, as we go line-by-line in this file, if the value in column 7 is equal to 6 then the match is true and the line is included in the output. I’m piping, using the | operator, the results to head to keep the output concise for this blog.

Now we want to test the other part of the conditional on the Pos column. This time we will use the >= operator to test if the values in the column 8 are greater than or equal to 11000000.

So far, we’ve confirmed that we can use the if statement in AWK to return the rows that meet our conditional expressions when true. Check out the documentation on using control statements in AWK for more ways you can use conditionals to make decisions.

The next step is to combine these conditional expressions with the third (less than 25000000) to do all the filtering in one pass. To do this we need to use boolean operators with our conditional expressions. Let’s try it for the two conditional expressions we worked out above first.

We are using the boolean and && here (other operators are || for or and ! for not) to combine our two conditional statements. Now let’s add our second column $8 condition ( awk -f source-file . There are many more features in the AWK language I didn’t discuss in this blog. The big takeaway here is that if you run into a file that exceeds or slows down memory bound languages like R, you can use stream based operations on those files in AWK.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

1 Answer 1

EXAMPLE: Grepping n lines for m regular expressions.

The simplest solution to grep a big file for a lot of regexps is:

Or if the regexps are fixed strings:

There are 3 limiting factors: CPU, RAM, and disk I/O.

RAM is easy to measure: If the grep process takes up most of your free memory (e.g. when running top), then RAM is a limiting factor.

CPU is also easy to measure: If the grep takes >90% CPU in top, then the CPU is a limiting factor, and parallelization will speed this up.

It is harder to see if disk I/O is the limiting factor, and depending on the disk system it may be faster or slower to parallelize. The only way to know for certain is to test and measure.

Limiting factor: RAM

The normal grep -f regexs.txt bigfile works no matter the size of bigfile, but if regexps.txt is so big it cannot fit into memory, then you need to split this.

grep -F takes around 100 bytes of RAM and grep takes about 500 bytes of RAM per 1 byte of regexp. So if regexps.txt is 1% of your RAM, then it may be too big.

If you can convert your regexps into fixed strings do that. E.g. if the lines you are looking for in bigfile all looks like:

then your regexps.txt can be converted from:

This way you can use grep -F which takes around 80% less memory and is much faster.

If it still does not fit in memory you can do this:

The 1M should be your free memory divided by the number of CPU threads and divided by 200 for grep -F and by 1000 for normal grep. On GNU/Linux you can do:

If you can live with duplicated lines and wrong order, it is faster to do:

Limiting factor: CPU

If the CPU is the limiting factor parallelization should be done on the regexps:

The command will start one grep per CPU and read bigfile one time per CPU, but as that is done in parallel, all reads except the first will be cached in RAM. Depending on the size of regexp.txt it may be faster to use –block 10m instead of -L1000.

Some storage systems perform better when reading multiple chunks in parallel. This is true for some RAID systems and for some network file systems. To parallelize the reading of bigfile:

This will split bigfile into 100MB chunks and run grep on each of these chunks. To parallelize both reading of bigfile and regexp.txt combine the two using –fifo:

If a line matches multiple regexps, the line may be duplicated.

Bigger problem

If the problem is too big to be solved by this, you are probably ready for Lucene.

FILTER is used by Data > Select Cases [ Details] ; it in fact generates automatically a command sequence like this one: USE ALL.

A filter is automatically turned off:

  1. If you read in a new data file.
  2. Use it after a TEMPORARY command.
  3. By the USE command.
  1. Each file in Linux has a corresponding File Descriptor associated with it.
  2. The keyboard is the standard input device while your screen is the standard output device.
  3. “>” is the output redirection operator. “>>” …
  4. “ &”re-directs output of one file to another.

3 Answers 3

The simplest solution I was able to find is (yes, that simple):

There are several possible solutions to this.

grep is quite fast at finding text, it only needs the correct regex.

  • The ^([^ ]* ) <5>part will match columns (not spaces) separated by spaces (5 ( <5>) of them), from the start ( ^ ) of a line.
  • Then, .*(=0.00000000.*) <2>will match at least two =0.00000000 on that line.
  • Finally, reverse the match ( -v ) and use extended (ERE) regexes (less needed).

It will be strict on the number of 0 s it will match.

Sed with a similar regex:

but it will print any line that fails to match the pattern (easy to fail positive).

Awk treating the line as text.

Awk, which could parse floating numbers and then check for 0 value.

Use @GlennJackman answer, please.

Using space or = as the field separator, start counting for zero values from column 7: if there are more then one, go on to the next line, else print the line.

This is the easiest way to print the lines that don’t have more than one instance of that string:

As your file only has that string appearing after column 5 and you only want to print the rows where it appears one time or not at all, the above will print the lines where it doesn’t appear more than once. The pattern =0.00000000.*0.00000000 matches any two instances of =0.00000000 on one line no matter in what columns they appear and if there is a third, fourth, fifth, and so on anywhere on one line, it won’t print the line. The command that you were trying prints any lines that don’t contain any instances of that string so it doesn’t print the second line which is what you didn’t want.

If you want it to print the lines that don’t contain more instances of that string, simply add another .*0.00000000 . For example, to print lines that don’t contain more than three:

That will include line three which contains three instances of that string.

unix command to filter data from a file

unix command to filter data from a file

3.3. Filtering Data¶ Linux for Programmers and Users, Section 4.2 – 4.3. We use commands that filter data to select only the portion of data that we wish to view or operate on. Filtering commands are usually run with either a filename as a command line argument or they read data from stdin, usually from a pipe.

3.Translate Command (tr) in unix : Tr command is also mostly used unix Filter Commands which stands for translate and used to translate file character by character. Syntax : Tr[Options] Data set1..Data set2…Data set N. Options of Translate command :-d -Which deletes the Characters in data set 1.

Filters are commands that always read their input from ‘stdin’ and write their output to ‘stdout’. Users can use file redirection and ‘pipes’ to setup ‘stdin’ and ‘stdout’ as per their need. Pipes are used to direct the ‘stdout’ stream of one command to the ‘stdin’ stream of the next command.

Linux and UNIX like operating systems do not store file creation time. However, you can use file access and modification time and date to find out file by date. For example, one can list all files that have been modified on a specific date.

bash filter array

In this challenge, we practice reading and filtering an array. Resources Here’s a great tutorial with useful examples related to arrays in Bash. Task You are given a list of countries, each on a new line. Your task is to read them into an array and then filter out (remove) all the names containing the letter ‘a’ or ‘A’.

Bash Array – An array is a collection of elements. Unlike in many other programming languages, in bash, an array is not a collection of similar elements. Since bash does not discriminate string from a number, an array can contain a mix of strings and numbers.

while [ «$column» -lt «$Columns» ] do let «index = $row * $Rows + $column» echo -n «$ » # alpha[$row][$column] let «column += 1» done let «row += 1» echo done # The simpler equivalent is # echo $ | xargs -n $Columns echo > filter # Filter out negative array indices. < echo -n " " # Provides the tilt.

To access the properties of elements in an array, you do one of two operations: flattening and filtering. This section covers how to flatten an array. Flattening an array is done with the [] JMESPath operator. All expressions after the [] operator are applied to each element in the current array.

pg command in unix

pg is a terminal pager program on Unix and Unix-like systems for viewing text files. It can also be used to page through the output of a command via a pipe. pg uses an interface similar to vi, but commands are different. As of 2018, pg has been removed from the POSIX specification, but is still included in util-linux.

DESCRIPTION pg displays a text file on a CRT one screenful at once. After each page, a prompt is displayed. The user may then either press the newline key to view the next page or one of the keys described below. If no filename is given on the command line, pg reads from standard input.

This subchapter looks at less, more, and pg, a related family of UNIX (and Linux) commands. less, more, and pgare utilities for reading a very large text file in small sections at a time. pgis the name of the historical utility on BSD UNIX systems. moreis the name of the historical utility on System V UNIX systems.

Pg displays a text file on a CRT one screenful at once. After each page, a prompt is displayed. The user may then either press the newline key to view the next page or one of the keys described below. If no filename is given on the command line, pg reads from standard input.

The grep command searches a file or files for lines that have a certain pattern. The syntax is − $grep pattern file (s) The name «grep» comes from the ed (a Unix line editor) command g/re/p which means “globally search for a regular expression and print all lines containing it”.

0 0 votes
Calificación del artículo
Ссылка на основную публикацию
Would love your thoughts, please comment.x