Filter and Pipe in Unix / Linux

You can connect two commands together so that the output of a program is the input of the next program. Two or more commands connected in this way form a pipe .

To create a pipe, place a vertical bar (|) on the command line between the two commands.

When a program receives input from another program, performs several operations on that input, and writes the output to the output, it is referred to as a filter .

The grep command in Unix / Linux

The grep program finds a file or files for the lines that have a certain pattern. Its syntax is:

 $ grep pattern file ( s ) 

The name grep derives from the ed command (UNIX line editor), g / re / p means "globally search for a regular expression and print all lines containing it" (search global for a regular expression and print all lines containing it)."

A regular expression is either some plain text or special characters used for pattern connections.

The simplest use of grep is to search for a template containing a single word. It can be used in a pipe so that only those lines of input files contain a given string sent to the standard output. If you don't give grep a file name to read, it reads the standard input, this is how all filters 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 different functions that you can use in parallel with the grep command:

Function Description -v Print all lines without matching the given pattern. -n Print matched lines and its line numbers. -l Print only the file name with matching lines. -c Only print the number of matched lines. -i Matches either with uppercase or lowercase letters.

Now, we use a regular expression that tells the grep command to find lines with "carol", followed by no more or more characters summarized in a regular expression as a "*" asterisk, then followed by "Aug".

Here we are using the -i function to have a case-sensitive search:

 $ ls - l | grep - i "carol. * aug" - rw - rw - r - 1 carol doc 1605 Aug 23 07 : 35 macros $ 

Sort command in Unix / Linux

The sort command sorts the lines of text in alphabetical or numerical order. The example below ranks the lines in a food file.

 $ sort food Afghani Cuisine Bangkok Wok Big Apple Deli Isle of Java Mandalay Sushi and Sashimi Sweet Tooth Tio Pepe 's Peppers $ 

Sort , by default, sorts the lines of text in alphabetical order. There are many functions that control this alignment:

Function Description -n Sort by numeric order, skip spaces and tabs. -r Inverse order of arrangement. -f Arrange uppercase and lowercase letters together. + x Ignore the first x fields when sorting.

There may be more than two commands connected in a pipe . The pipe example uses grep , we can sort files in a different way by editing the order of size in August.

The following pipe contains 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 the files in your directory edited in order of size in August and prints them to the terminal screen. Function + 4n ignores 4 fields (fields that are distinguished by spaces), then sorts the lines in numerical order.

The pg commands and more in Unix / Linux

A long output often overflows the screen, but if you run text through more or pg as a filter, the display will stop after each screen is full of text.

We assume that you have a long directory list. To make it easier to read this sorted list, output the output via the more command 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 be filled with the text contained in the lines arranged in size order. At the bottom of the screen, at the more prompt you can type a command to move through the sorted text.

When you work with this screen, you can use any of the commands listed in the program.

According to Tutorialspoint

Previous article: Basic utilities: print, email in Unix

Next lesson: Process management in Unix / Linux

4.5 ★ | 2 Vote

May be interested

  • Process management in Unix / LinuxPhoto of Process management in Unix / Linux
    when you run a program on a unix system, the system creates a special environment for that program. this environment contains everything needed for the system to run the program if no other program is running on the system.
  • Network communication utilities in Unix / LinuxPhoto of Network communication utilities in Unix / Linux
    when you work in a distribution environment then you need to communicate with remote users and you also need access to remote unix devices.
  • Micro editor in Unix / LinuxPhoto of Micro editor in Unix / Linux
    there are many ways to edit files in unix and for me, one of the best ways is to use the editor to edit the micro screen orientation. this editor lets you edit the lines of content with other lines in the file.
  • What is a shell?Photo of What is a shell?
    shell is an environment in which we can run commands, programs and shell scripts. there are different versions of shell, which differ only from the version of the operating system. each version of shell has its own set of recognized commands and functions.
  • Use variables in ShellPhoto of Use variables in Shell
    a variable is a string of characters from which we assign a value. the assigned value can be a number, text, file name, device or any other type of data.
  • Special variables in Unix / LinuxPhoto of Special variables in Unix / Linux
    the previous tutorial has warned about using special characters in your variable name. this is because these characters are used in the names of special variables in unix. these variables are kept for special functions.