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
You should read it
- Basic utilities: print, email in Unix
- Navigation IO in Unix / Linux
- Regular Expression in Unix / Linux
- Useful commands in Unix / Linux
- Basic file system in Unix / Linux
- System log in Unix / Linux
- What is a shell?
- Control loop in Unix / Linux
- Network communication utilities in Unix / Linux
- 14 interesting Linux commands in Terminal
- ManPage Help in Unix
- What is Unix / Linux?
Maybe you are interested
Bitcoin price today August 1: Adjusted to 64,300 USD
The PSVR 2 Steam app will launch this August
Apple, Nvidia, and many large companies are caught up in AI training scandals using controversial YouTube sources
Google goo․gl links will stop working in August 2025
[Caught sick] iPhone screen appears blurred circle
Microsoft wants users to switch to the new Outlook before August, or receiving emails will be interrupted