Catalog / Text Processing Tools Cheatsheet: grep, sed, awk
Text Processing Tools Cheatsheet: grep, sed, awk
A quick reference for using grep, sed, and awk for text processing in Linux/Unix environments, covering essential commands, options, and syntax with practical examples.
grep - Global Regular Expression Print
Basic Usage
|
Search for ‘pattern’ in ‘file’ and print matching lines. Example:
|
|
Case-insensitive search. Example:
|
|
Invert the match: print lines that do not contain the pattern. Example:
|
|
Print the line number with each matching line. Example:
|
|
Print only a count of matching lines. Example:
|
|
List only the names of files that contain the pattern. Example:
|
|
Suppress the prefixing of filenames on output when multiple files are searched. Example:
|
Advanced grep
|
Use extended regular expressions. Example:
|
|
Search for whole words only. Example:
|
|
Recursively search through files in a directory. Example:
|
|
Print only the matching part of the lines. Example:
|
|
Print ‘n’ lines after the matching line. Example:
|
|
Print ‘n’ lines before the matching line. Example:
|
|
Print ‘n’ lines around the matching line (context). Example:
|
Regular Expressions in grep
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
sed - Stream EDitor
Basic sed Commands
|
Replace all occurrences of ‘old’ with ‘new’ in ‘file’. Example:
|
|
Replace the first occurrence of ‘old’ with ‘new’ in each line. Example:
|
|
Case-insensitive replacement (GNU sed). Example:
|
|
Delete lines containing ‘pattern’. Example:
|
|
Delete the second line. Example:
|
|
Delete the last line. Example:
|
|
Delete lines 1 to 3. Example:
|
|
Delete from line 1 to the first line containing ‘pattern’. Example:
|
sed Substitution Flags
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
sed - Advanced
|
Suppress automatic printing and print only specific lines. Example:
|
|
Print only lines matching a pattern. Example:
|
|
Translate ‘a’ to ‘x’, ‘b’ to ‘y’, ‘c’ to ‘z’. Example:
|
|
Modify the file in-place (use with caution!). Example:
|
|
Insert ‘new line’ before the first line Example:
|
|
Append ‘new line’ after the last line Example:
|
awk - Pattern Scanning and Processing Language
Basic awk Syntax
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
awk Built-in Variables
|
Number of fields in the current record. Example:
|
|
Number of the current record (line number). Example:
|
|
Field separator (default is whitespace). Example:
|
|
Output field separator (default is whitespace). Example:
|
|
Output record separator (default is newline). Example:
|
awk - Conditional Statements and Loops
Example:
|
Example:
|
Example:
|
Example:
|
Combining Tools
Piping and Redirection
Example:
|
Example:
|
Example:
|
Example:
|
Complex Examples
Extract all unique IP addresses from a log file:
|
Count the occurrences of each word in a file:
|
Find lines in a file that do not contain a specific word:
|
Replace all occurrences of a pattern in multiple files:
|
Calculate the average of a column in a CSV file:
|