Intro to Filters
Text Filters
One of the most common, and most useful, types of command line applications is a “filter.”
A filter is any application which accepts text as its input, and provides the text, or a modified version of it, as its output.
A simple example of this is the sort
command.
Example file:
fruit.txt
bananas
kiwis
apples
tomatoes
oranges
> sort fruit.txt
apples
bananas
kiwis
oranges
tomatoes
Filters can do anything you can imagine, including modifying the output.
Let’s see a couple examples of the grep
command:
> grep e fruit.txt
apples
tomatoes
oranges
> grep an fruit.txt
bananas
oranges
In the first example, we look for any lines containing the letter e
.
In the second, we look for the letters an
.
Filters can be used, among other things to:
- search
- Return only results matching search query.
- reformat
- Modify text. For example, but standardizing phone numbers from
5552121234
to(555) 212-1234
. - replace
- Replace abbreviations with full words, or even placeholder text with templates.