DEV Community

Cover image for How to Use grep, awk, and sed Like a SysAdmin
pawan natekar
pawan natekar

Posted on

How to Use grep, awk, and sed Like a SysAdmin

If you work with Linux, you will eventually spend time reading logs, searching configuration files, and cleaning messy text output.

At first, many people try to do this manually.

Open file. Scroll. Search. Copy text. Repeat.

That works for small tasks.

But sysadmins do not work that way.

They use command-line tools that solve problems in seconds.

Three of the most useful tools are:

  • grep
  • awk
  • sed

If you learn these well, your Linux troubleshooting speed improves a lot.

This post shows practical examples, not textbook definitions.

How to Use grep, awk, and sed Like a SysAdmin

Why These Three Commands Matter
Imagine these real situations:

You want to find failed SSH login attempts.

You want to extract IP addresses from logs.

You want to replace a wrong server name in a config file.

You want to count repeated entries.

Doing this manually wastes time.

This is where these commands help.

Think of them like this:

  • grep = find text
  • awk = extract and process columns
  • sed = edit and transform text

1. grep: Find What Matters Fast
grep searches for matching text.

Basic syntax:

grep "pattern" filename
Enter fullscreen mode Exit fullscreen mode

Example:

grep "error" app.log
Enter fullscreen mode Exit fullscreen mode

Output:

database connection error
api timeout error
Enter fullscreen mode Exit fullscreen mode

This finds lines containing the word error.

Useful grep Options

Ignore case

grep -i "error" app.log
Enter fullscreen mode Exit fullscreen mode

Matches:

  • Error
  • ERROR
  • error

Helpful when log formats are inconsistent.

Show line numbers

grep -n "server" nginx.conf
Enter fullscreen mode Exit fullscreen mode

Output:

12:server_name example.com;
45:server_tokens off;
Enter fullscreen mode Exit fullscreen mode

Good for config debugging.


Invert match

Show lines that do NOT match:

grep -v "INFO" app.log
Enter fullscreen mode Exit fullscreen mode

Useful when removing noisy logs.


Recursive search

Search inside directories:

grep -r "Listen 80" /etc/apache2
Enter fullscreen mode Exit fullscreen mode

Very useful for config hunting.

Real SysAdmin Example

Find failed SSH login attempts:

grep "Failed password" /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode

Sample output:

Failed password for root from 192.168.1.10
Failed password for admin from 10.0.0.5
Enter fullscreen mode Exit fullscreen mode

This quickly shows suspicious login attempts.

2. awk: Extract and Process Data

awk is excellent when data has columns.

Example file:

alice 5000 IT
bob 7000 HR
john 6500 DevOps
Enter fullscreen mode Exit fullscreen mode

Print first column:

awk '{print $1}' employees.txt
Enter fullscreen mode Exit fullscreen mode

Output:

alice
bob
john
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • $1 = first column
  • $2 = second column
  • $3 = third column

Print Multiple Columns

awk '{print $1, $3}' employees.txt
Enter fullscreen mode Exit fullscreen mode

Output:

alice IT
bob HR
john DevOps
Enter fullscreen mode Exit fullscreen mode

Filter by Condition

Show salaries above 6000:

awk '$2 > 6000 {print $1, $2}' employees.txt
Enter fullscreen mode Exit fullscreen mode

Output:

bob 7000
john 6500
Enter fullscreen mode Exit fullscreen mode

This is very useful for reports.

Real SysAdmin Example

Check logged-in users:

who
Enter fullscreen mode Exit fullscreen mode

Example output:

pawan pts/0 2026-05-18 10:30
john pts/1 2026-05-18 11:00
Enter fullscreen mode Exit fullscreen mode

Extract usernames:

who | awk '{print $1}'
Enter fullscreen mode Exit fullscreen mode

Output:

pawan
john
Enter fullscreen mode Exit fullscreen mode

3. sed: Stream Editing Made Simple

sed helps modify text.

Basic replacement:

sed 's/old/new/' file.txt
Enter fullscreen mode Exit fullscreen mode

Example:

sed 's/dev/prod/' config.txt
Enter fullscreen mode Exit fullscreen mode

If file contains:

server=dev
Enter fullscreen mode Exit fullscreen mode

Output:

server=prod
Enter fullscreen mode Exit fullscreen mode

Replace All Matches

Without global flag, only first match changes.

Use:

sed 's/error/warning/g' app.log
Enter fullscreen mode Exit fullscreen mode

g = global replacement

Delete Lines

Delete blank lines:

sed '/^$/d' file.txt
Enter fullscreen mode Exit fullscreen mode

Very useful when cleaning files.

Edit File Directly

sed -i 's/localhost/db-server/' config.ini
Enter fullscreen mode Exit fullscreen mode

Be careful.

This changes the actual file.


Real SysAdmin Example

Update nginx config:

Before:

server_name oldsite.com;
Enter fullscreen mode Exit fullscreen mode

Command:

sed -i 's/oldsite.com/newsite.com/' nginx.conf
Enter fullscreen mode Exit fullscreen mode

Fast and practical.

Combining Commands Like a SysAdmin

The real power comes from combining tools.

Example:

Find failed SSH attempts and extract IP addresses:

grep "Failed password" /var/log/auth.log | awk '{print $11}'
Enter fullscreen mode Exit fullscreen mode

Output:

192.168.1.10
10.0.0.5
Enter fullscreen mode Exit fullscreen mode

Count repeated IPs:

grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c
Enter fullscreen mode Exit fullscreen mode

Sample output:

5 192.168.1.10
2 10.0.0.5
Enter fullscreen mode Exit fullscreen mode

Now you know which IP is attacking most.

This is real troubleshooting.

Common Beginner Mistakes

Using grep for everything

Yes, grep is useful.

But if you need column processing, use awk.

Editing files with sed without backup

This:

sed -i 's/test/prod/' file.conf
Enter fullscreen mode Exit fullscreen mode

changes the file immediately.

Safer:

cp file.conf file.conf.bak
Enter fullscreen mode Exit fullscreen mode

then edit.

Forgetting quotes

Wrong:

grep error file.txt
Enter fullscreen mode Exit fullscreen mode

Better:

grep "error" file.txt
Enter fullscreen mode Exit fullscreen mode

Especially for complex patterns.

Quick Comparison

Tool Best Use
grep Search matching text
awk Extract/process columns
sed Replace/edit text

Top comments (0)