Know Your Pipes and Filters

Pipes and filters are everywhere. Not just in your homes but also in the terminal.

Piyush Kochhar
The Startup
8 min readAug 4, 2020

--

Know your pipes and filters — Created by Piyush Kochhar

What is a Filter? ䷜

A filter takes input from one command, does some processing, and gives output.

Why filter data? 🗂

Filtering is useful when you want to extract specific information from large amounts of data. It doesn’t remove or modify any data; it just displays a subset of data on the screen. Sometimes we use pipes to filter complex data.

What is a Pipe? 🚰

The symbol ‘|’ denotes a pipe. It is a command that takes an output of one command and feeds it as an input to the second command. You can combine two or more commands and run them consecutively with pipes.

Syntax:

Suppose we have a sample.txt file with the following data:

Let’s filter some data!

😸 cat : Displays the contents of the file.

⫷ less: Shows you only one scroll length of content at a time.

⥱ grep: Takes an argument as string and will find the matching string, if found it will return the matching string else will return nothing.

Following options can be used with grep command:

-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 → Match both (upper and lower) case

-A(num) → Print NUM lines of trailing context after matching lines.

-B(num) → Print NUM lines of leading context before matching lines.

-C(num) → Print NUM lines of output context.

-v

-c

-n

-i

-A(num)

-B(num)

-C(num)

⋛ sort: Sorts out the contents of a file.

Following options can be used with sort command:

-r → Reverse sorting

-n → Numerical sorting

-f → Case insensitive sorting

-r

-n

-f

👓 uniq: Omits repeated lines.

Following option can be used with uniq command:

-c → Indicates the number of occurrences of a line.

☕️ tee: Reads from the standard input and writes to both standard output and one or more files at the same time.

The above command creates newfile1.txt and newfile2.txt with the text “New text here”.

Following option can be used with tee command:

-a → Appends the output to a file instead of overwriting it.

Use /dev/null to hide the standard output

✂️ cut: Slices a line and extracts the text. It is necessary to specify option with command otherwise it gives error.

Following options can be used with cut command

-b → Extracts specific bytes

-c → Cuts the characters by column

-f → Cuts the character to the delimiter character with the specified field number

-d → delimiter, mostly used in conjunction with -f

Filter the text to get 1st, 4th & 8th character using -b

Filter the text to get characters in range [1–4] & [9–11] using -b

Filter text to get characters in column [1–3] using -c

Filter text to get last name using -d

✏️ fmt: A Simple optimal text formatter, it reformats paragraphs in specified file.

Following option can be used with fmt command:

-w → Reformat the text with a specific line width

🤯 head: Displays the first parts of a file, it outputs the first 10 lines by default.

Following option can be used with head command:

-n → Number of lines to be displayed

🌀 tail: Displays the last parts of a file, it outputs the last 10 lines by default.

Following option can be used with tailcommand:

-n → Number of lines to be displayed

📝 tr: Translates or deletes characters from standard input.

Lowercase to uppercase

Note: We can also use regular expressions to translate the data.

Apply ROT13 cipher to translate the data. To know more about ROT13 visit here.

⏳ sed: A stream editor for filtering and transforming text.

We can use regular expressions to transform our text.

To replace a substring: ‘s/text to replace/replacement text

To replace the nth occurrence: ‘s/text to replace/replacement text/occurence_no

To replace all occurrences: ‘s/text to replace/replacement text/g

To delete a line: ‘(line num)d’

For nth line replace ‘2’ with the number of your line

To delete lines from range x to y: ‘(first line), (last line)d’

To delete last line: ‘$d’

To delete a line that matches a pattern: ‘pattern’

✍️ od: Returns contents of a file in specified number system. It returns hexadecimal by default.

Get output in octal using -b

Get output in character format using -c

✣ wc: Returns line, word and character count.

Hope you are now better at understanding pipes and filters!

--

--