Pipe, Grep and Sort Command in Linux/Unix with Examples

โšก Smart Summary

Pipe, grep, and sort commands in Linux and Unix chain and filter text: a pipe feeds one command’s output into the next, grep searches for patterns, and sort arranges lines, while filters transform the data between them.

  • ๐Ÿ”— Pipe: The pipe symbol | passes the output of one command as the input to the next.
  • ๐Ÿ“„ Paging: cat piped to less, pg, or more shows a long file one screen at a time.
  • ๐Ÿ” grep: The grep command searches a file for lines that match a string or pattern.
  • ๐ŸŽš๏ธ grep options: Flags such as -v, -c, -n, -i, and -l invert, count, number, ignore case, and list files.
  • ๐Ÿ”ค sort: The sort command orders a file’s lines, with -r, -n, and -f for reverse, numeric, and case-insensitive.
  • ๐Ÿงช Filters: Filter commands such as grep, sort, awk, sed, and wc process piped input and produce output.
  • ๐Ÿค– AI help: AI assistants and GitHub Copilot can build and explain pipe, grep, and sort pipelines.

Pipe, grep, and sort commands in Linux and Unix with examples

Pipes, grep, and sort are among the most-used tools on the Linux command line, because they let you connect small commands into one powerful workflow. This page explains each one with worked examples, then shows how filters tie them together.

What is a Pipe in Linux?

The pipe is a command in Linux that lets you use two or more commands such that the output of one command serves as input to the next. In short, the output of each process feeds directly as input into the next one, like a pipeline. The symbol ‘|’ denotes a pipe.

Pipes help you mash up two or more commands at the same time and run them consecutively. You can use powerful commands that perform complex tasks in a jiffy.

Let us understand this with an example.

When you use the ‘cat’ command to view a file that spans multiple pages, the prompt quickly jumps to the last page of the file, and you do not see the content in the middle.

To avoid this, you can pipe the output of the ‘cat’ command to ‘less’, which shows you only one scroll length of content at a time.

cat filename | less

An illustration would make it clear. The diagram below shows how a pipe passes the output of one command into the next.

Diagram of a Linux pipe passing the output of one command as input to the next

‘pg’ and ‘more’ commands

Instead of ‘less’, you can also use the pg or more command:

cat Filename | pg

or

cat Filename | more

You can then view the file in digestible bits and scroll down by simply hitting the enter key. The screenshot below shows a file paged with the more command.

cat piped to the more command paging a long file one screen at a time

The ‘grep’ command

Suppose you want to search for particular information, such as a postal code, in a text file.

You may manually skim the content yourself to trace the information. A better option is to use the grep command. It scans the document for the desired information and presents the result in the format you want.

Syntax:

grep search_string

Let us see it in action. The screenshot below shows the result of the search.

grep command searching the sample file for the strings Apple and Eat

Here, the grep command has searched the file ‘sample’ for the strings ‘Apple’ and ‘Eat’. The following options can be used with this command.

Option Function
-v Shows all the lines that do not match the searched string
-c Displays only the count of matching lines
-n Shows the matching line and its number
-i Matches both upper and lower case
-l Shows just the name of the file with the string

Let us try the ‘-i’ option on the same file used above. The screenshot below shows the case-insensitive result.

grep -i performing a case-insensitive search for the letter a

Using the ‘-i’ option, grep has filtered the string ‘a’ (case-insensitive) from all the lines.

The ‘sort’ command

This command helps in sorting out the contents of a file alphabetically.

The syntax for this command is:

sort Filename

Consider the contents of a file, shown with cat below.

cat command showing the unsorted contents of the file abc

Running the sort command arranges those lines in alphabetical order, as shown below.

sort command arranging the contents of file abc in alphabetical order

There are extensions to this command as well, and they are listed below.

Option Function
-r Reverses sorting
-n Sorts numerically
-f Case-insensitive sorting

The example below shows reverse sorting of the contents in file ‘abc’.

sort -r command reverse sorting the contents of file abc

What is a Filter?

Linux has a lot of filter commands such as awk, grep, sed, spell, and wc. A filter takes input from one command, does some processing, and gives output.

When you pipe two commands, the ‘filtered’ output of the first command is given to the next. The diagram below illustrates how a filter fits into a pipeline.

Diagram of a Linux filter taking piped input, processing it, and producing output

Let us understand this with the help of an example. We have the following file ‘sample’, shown with cat below.

cat command displaying the contents of the sample file before filtering

We want to highlight only the lines that do not contain the character ‘a’, but the result should be in reverse order. For this, the following syntax can be used:

cat sample | grep -v a | sort -r

Let us look at the result, shown in the screenshot below.

cat piped through grep -v and sort -r producing reverse-ordered lines without the letter a

FAQs

A pipe (|) sends the output of one command straight into another command, chaining them together. Redirection (> and <) connects a command to a file instead, writing output to a file or reading input from one. Pipes link commands; redirection links commands and files.

grep stands for Global Regular Expression Print. It scans a file or input line by line and prints every line that matches the pattern you give it. The name comes from the old ed editor command g/re/p, which performed the same search.

The sort command has a -u option that sorts and removes duplicate lines in one step: sort -u file. You can also pipe sort into uniq, as in sort file | uniq, since uniq only removes duplicates that are already adjacent.

End the pipeline with redirection: cat sample | grep -v a | sort -r > result.txt writes the final output to a file. To see the output on screen and save it at the same time, pipe into the tee command instead.

grep searches inside files for lines that match a text pattern. The find command locates files and directories themselves by name, size, type, or date. In short, grep looks at file contents, while find looks for the files across the directory tree.

The tee command reads from a pipe and writes the data to both the screen and one or more files at once, for example grep error log | tee errors.txt. It lets you save intermediate output without breaking the pipeline.

AI assistants can suggest the right chain of piped commands from a plain-English request, explain unfamiliar grep or sort flags, and spot mistakes in a pipeline. Machine-learning tools also scan piped log output to flag anomalies that manual filtering would miss.

Yes. GitHub Copilot can generate pipelines that combine cat, grep, and sort from a short comment describing what you need, and complete commands as you type. Review any suggested pipeline against sample data, especially options that reorder or delete lines.

Summarize this post with: