惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
博客园 - 聂微东
爱范儿
爱范儿
罗磊的独立博客
P
Proofpoint News Feed
博客园 - Franky
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
美团技术团队
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
I
InfoQ
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Use grep, awk, and sed Like a SysAdmin
pawan nateka · 2026-05-19 · via DEV Community

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