The grep, awk, and sed commands are powerful text-processing tools in Linux. They are commonly used in Bash scripts to search, filter, extract, and modify text. If you are learning Bash scripting, these three commands are very important because many real-world scripts work with log files, configuration files, command output, and reports.
For Linux administration and cybersecurity tasks, grep, awk, and sed are extremely useful. You can use them to find failed login attempts, extract IP addresses, filter scan results, replace text in files, and analyze system logs.
The grep command is used to search for text patterns inside files or command output.
Basic syntax:
grep "pattern" filename
Example:
grep "root" /etc/passwd
This searches for the word root inside the /etc/passwd file.
You can also use grep with command output:
ps aux | grep apache
This searches running processes for apache.
Create a script:
nano grep-example.sh
Add this code:
#!/bin/bashlog_file="/var/log/auth.log"echo "Checking failed SSH logins..."grep "Failed password" "$log_file"
Run it:
chmod +x grep-example.sh./grep-example.sh
This script searches the authentication log for failed SSH login attempts.
The awk command is used to process columns and fields from text. It is very useful when working with structured output.
Basic syntax:
awk '{print $1}' filename Example:
awk '{print $1}' /etc/passwd This prints the first field from each line. By default, awk separates fields using spaces.
To use a custom separator, use -F.
awk -F: '{print $1}' /etc/passwd This prints usernames from /etc/passwd.
#!/bin/bashecho "System users:"awk -F: '{print $1}' /etc/passwd This script lists all local usernames from the system password file.
The sed command is used to edit text automatically. It is commonly used to replace words, delete lines, or modify file content.
Basic syntax:
sed 's/old/new/' filename
Example:
echo "I like Linux" | sed 's/Linux/Bash/'
Output:
I like Bash
#!/bin/bashecho "server=old-domain.com" > config.txtsed 's/old-domain.com/new-domain.com/' config.txt
This replaces old-domain.com with new-domain.com in the output.
To edit the file directly, use:
sed -i 's/old-domain.com/new-domain.com/' config.txt
#!/bin/bashlog_file="/var/log/auth.log"echo "Extracting IP addresses from failed SSH logins..."grep "Failed password" "$log_file" | awk '{print $(NF-3)}' | sort | uniq This script searches failed SSH login lines, extracts possible IP addresses, sorts them, and removes duplicates.
The grep, awk, and sed commands are essential for Bash scripting. grep searches text, awk extracts fields, and sed edits text. Together, they help you process logs, files, command output, and reports efficiently.
For beginners, learning these tools will improve your Bash scripting skills and help you build practical Linux automation and cybersecurity scripts
phpMyAdmin is a free, open-source PHP application that provides a browser-based interface for managing MySQL and…
Zabbix is a mature open-source infrastructure monitoring platform that collects metrics from network devices, servers, virtual…
Gradle is a powerful open-source build automation tool used primarily for Java, Kotlin, Groovy, and Android…
TeamViewer is a proprietary cross-platform remote access application for remote control, desktop sharing, file transfer, and online meetings. It is one of the most widely used remote support tools in the world, available for Windows, macOS, Linux, iOS, and Android. TeamViewer is not included in the Ubuntu repositories because it is proprietary software. This guide covers how to install TeamViewer on Ubuntu 18.04 using the official .deb package. The same steps apply to Ubuntu 16.04, Debian, Linux Mint, and Elementary OS. <strong>Prerequisite:</strong> You need sudo access. Install TeamViewer on Ubuntu: Download the .deb Package Download the official TeamViewer .deb package. The _amd64.deb suffix indicates this package is for 64-bit x86-64 systems. For ARM-based machines, download the appropriate package from the TeamViewer Linux downloads page: bashwget https://download.teamviewer.com/download/linux/teamviewer_amd64.deb Install the package using apt. The ./ prefix tells apt this is a local file path, not a package name from the repositories:…
Nagios is one of the most widely used open-source infrastructure monitoring systems in the world. It…
Laravel is an open-source PHP web application framework built around an expressive, developer-friendly syntax. It is…