USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Search, Filtering, and Text Processing

Lesson 19/32 | Study Time: 20 Min

The command line in Linux provides powerful utilities for searching, filtering, and processing text, enabling users to manipulate data efficiently and extract meaningful information.

Whether handling logs, code, or large datasets, mastering these tools is essential for developers and system administrators aiming to automate tasks and analyze data quickly. 

Search Tools: Finding Text Patterns

grep (Global Regular Expression Print)

The most commonly used tool for searching plain-text data for lines matching a regular expression.


Basic usage: grep "pattern" filename

Recursive search: grep -r "pattern" directory/

Case-insensitive: grep -i

Show line numbers: grep -n

Invert match: grep -v (select non-matching lines)


Example:

text
grep -rin "error" /var/log/

Searches recursively, case-insensitive, showing line numbers.


ack, ag (the silver searcher), rg (ripgrep)

Tools optimized for speed and developer workflows supporting Gitignore files, recursive search, and colorful output.

Filtering and Selecting Text: cut, sort, uniq

cut: Extracts sections from each line of input, useful for column-based data.


Parameters:


-d defines delimiter

-f selects fields


Example:

text
cut -d ',' -f 1 file.csv



sort: Sorts lines alphabetically or numerically for ordering data.

Example:

text
sort file.txt
sort -nr numbers.txt # numeric reverse


uniq: Filters repeated adjacent lines to unique ones, often used after sorting.


Example:

text
sort file.txt | uniq
uniq -c file.txt # count occurrences

Text Processing Utilities: awk, sed, tr, paste


awk: A programming language for pattern scanning and processing.

It can filter records, perform calculations, and generate reports.


Example:

text
awk -F',' '{print $2, $3}' file.csv

Extracts and prints second and third fields.


sed (stream editor): Performs basic text transformations like substitution and deletion on input streams.


Example:

text
sed 's/old/new/g' file.txt

Replaces all instances of ‘old’ with ‘new’.


tr (translate): Translates or deletes characters. Useful for simple character-level transformations.


Example:

text
tr '[:lower:]' '[:upper:]' < file.txt

Converts lower case to upper case.


paste: Merges lines from multiple files horizontally, aligning columns.


Example:

text
paste file1.txt file2.txt > merged.txt

Combining Commands with Pipes

Piping (|) connects the stdout of one command to the stdin of another, enabling complex text processing pipelines.


Example:

text
grep "error" logfile.txt | sort | uniq -c | sort -nr

Finds unique error messages and sorts by frequency.

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.