grep, awk, sed, top, htopThis guide summarizes the usage of key Linux commands for text processing and system monitoring.
grep β Search Text in FilesSearch for patterns using regular expressions.
Syntax:
grep [options] pattern [file]
Examples:
grep "error" logfile.txt # Find lines with "error"
grep -i "warning" syslog # Case-insensitive search
grep -r "TODO" ./project/ # Recursive search in directory
grep -v "success" output.log # Exclude lines containing "success"
awk β Pattern-Action Text ProcessorProcess and extract text by fields (columns).
Syntax:
awk 'pattern {action}' file
Examples:
awk '{print $1}' file.txt # Print first column
awk -F: '{print $1, $3}' /etc/passwd # Print user and UID
awk '$3 > 50 {print $1, $3}' data.txt # Print lines where 3rd column > 50
sed β Stream Editor (Modify Text)Edit text in-place using patterns.
Syntax:
sed [options] 'command' file
Examples:
sed 's/foo/bar/' file.txt # Replace first "foo" with "bar"
sed 's/foo/bar/g' file.txt # Replace all "foo" with "bar"
sed -i 's/localhost/127.0.0.1/g' conf.cfg # Edit file in place
sed -n '5,10p' file.txt # Print lines 5 to 10
top β Live System Resource MonitorShows real-time CPU, memory, and process info.
Usage:
top
Controls:
P β Sort by CPUM β Sort by memoryk β Kill a processq β Quithtop β Enhanced Top (Interactive Viewer)Colorful, user-friendly version of top. Not always pre-installed.
Usage:
htop
Features:
| Command | Purpose | Example |
|---|---|---|
grep |
Search lines by pattern | grep "root" /etc/passwd |
awk |
Extract/format columns | awk '{print $2}' data.txt |
sed |
Edit text in-place | sed 's/a/b/' file.txt |
top |
View real-time system stats | top |
htop |
Enhanced system monitor | htop |
grep, awk, and sed in pipelines:ps aux | grep apache | awk '{print $2}' | xargs kill
htop for better visualization and interaction.sed -i.