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:
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:
cut -d ',' -f 1 file.csvsort: Sorts lines alphabetically or numerically for ordering data.
Example:
sort file.txt
sort -nr numbers.txt # numeric reverseuniq: Filters repeated adjacent lines to unique ones, often used after sorting.
Example:
sort file.txt | uniq
uniq -c file.txt # count occurrencesText 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:
awk -F',' '{print $2, $3}' file.csvExtracts and prints second and third fields.
sed (stream editor): Performs basic text transformations like substitution and deletion on input streams.
Example:
sed 's/old/new/g' file.txtReplaces all instances of ‘old’ with ‘new’.
tr (translate): Translates or deletes characters. Useful for simple character-level transformations.
Example:
tr '[:lower:]' '[:upper:]' < file.txtConverts lower case to upper case.
paste: Merges lines from multiple files horizontally, aligning columns.
Example:
paste file1.txt file2.txt > merged.txtCombining Commands with Pipes
Piping (|) connects the stdout of one command to the stdin of another, enabling complex text processing pipelines.
Example:
grep "error" logfile.txt | sort | uniq -c | sort -nrFinds unique error messages and sorts by frequency.
We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.