Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Grep Command in Unix // Linux: Filter and Pipe in Unix // Linux

Understand Grep Command in Unix // Linux: Filter and Pipe in Unix // Linux with clear explanations, practical examples, and useful tips. This updated guide...

Table of Contents

Grep Command in Unix // Linux: Filter and Pipe in Unix // Linux is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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 %) 

Next lesson: Process management in Unix / Linux

FAQ

What should you know about 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:

What should you know about 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.

What should you know about 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.