1. grep
– Search for Patterns
-
Purpose: Searches for specific patterns in text using regular expressions.
-
Basic Usage:
grep "pattern" filename
-
Example:
grep "error" server.log
Finds all lines in
server.log
that contain the word “error”.
2. awk
– Pattern Scanning and Processing
-
Purpose: A powerful programming language for pattern scanning, text manipulation, and reporting.
-
Basic Usage:
awk '{print $1}' filename
-
Example:
awk '/error/ {print $2}' server.log
Prints the second field of lines that contain “error”.
3. sed
– Stream Editor
-
Purpose: Edits text in a stream, often used for substitution or deletion.
-
Basic Usage:
sed 's/old/new/' filename
-
Example:
sed 's/error/issue/' server.log
Replaces the first occurrence of “error” with “issue” in each line.
4. cut
– Extract Columns from Text
-
Purpose: Removes sections (fields or columns) from each line of input.
-
Basic Usage:
cut -d':' -f1 /etc/passwd
-
Example:
Outputs the first field (username) from/etc/passwd
.
5. sort
– Sort Lines of Text Files
-
Purpose: Sorts lines in text files alphabetically or numerically.
-
Basic Usage:
sort filename
-
Example:
sort -n numbers.txt
Sorts numbers in ascending numerical order.
6. uniq
– Report or Filter Repeated Lines
-
Purpose: Removes or displays duplicate lines (only works on sorted data).
-
Basic Usage:
uniq filename
-
Example:
sort data.txt | uniq -c
Sorts the file and shows each line’s frequency of occurrence.