Fundamentals 16 min read

Mastering the Linux Trio: Practical Grep, Sed, and Awk Techniques

This tutorial introduces the three essential Linux text‑processing tools—grep, sed, and awk—explains their core options and syntax, and provides concrete command‑line examples so readers can quickly improve file‑handling efficiency and operational productivity.

DataFunSummit
DataFunSummit
DataFunSummit
Mastering the Linux Trio: Practical Grep, Sed, and Awk Techniques

The article presents the "Linux three musketeers"—grep, sed, and awk—as indispensable command‑line utilities for fast text processing, explaining how mastering them can dramatically boost file‑handling speed and overall operational efficiency.

Grep is a line‑oriented search tool. Common options include -i (ignore case), -n (show line numbers), -c (count matches), and --color=auto . Example usage: grep -i "haodao" haodao.txt finds all case‑insensitive occurrences of "haodao"; grep -i -n "haodao" haodao.txt also prints line numbers; grep -i -c "haodao" haodao.txt returns the count of matching lines.

Sed (stream editor) performs in‑place editing, filtering, and transformation of text streams. Frequently used options are -n (suppress automatic output), -i (edit files directly), -e (multiple commands), and -r (extended regex). Typical commands include: sed -n '1,+4p' haotest01.txt (print lines 1‑5), sed 's/good/bad/g' haotest01.txt (replace "good" with "bad"), sed '3a we he he' -i haotest01.txt (append a line after line 3), and sed 'a *****' -i haotest01.txt (append five asterisks after every line).

Awk is a powerful pattern‑action language for column‑oriented processing. Its basic syntax is awk [options] 'pattern {action}' file . Useful options are -F (input field separator), -v (assign variables), and -f (script file). Built‑in variables such as $0 (entire line), $n (nth field), NF (field count), and NR (record number) enable flexible data extraction. Example commands: awk '{print $1,$NF}' haotest03.txt prints the first and last columns, and awk 'NR==2,NR==3{print NR,$0}' haotest02.txt outputs lines 2‑3 with line numbers.

The article concludes by urging readers to practice these commands repeatedly, emphasizing that mastery comes from hands‑on experience, and invites them to follow the "浩道linux" channel for more Linux, Python, and networking resources.

Linuxcommand linetext processinggrepawksed
DataFunSummit
Written by

DataFunSummit

Official account of the DataFun community, dedicated to sharing big data and AI industry summit news and speaker talks, with regular downloadable resource packs.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.