Mastering the Text Trio: grep, sed, and awk for Powerful Text Processing
This guide introduces the essential Unix text-processing trio—grep, sed, and awk—explaining their concepts, syntax, common options, regular expression fundamentals, and practical examples, enabling readers to efficiently search, edit, and transform text files directly from the command line.
The Text Trio: grep, sed, and awk
grep
Concept
grep is a powerful text-search tool that uses pattern matching (including regular expressions) to search files and outputs matching lines by default.
Syntax
grep [options] pattern file
Options
-i ignore case
-n show line numbers
-c count matching lines
-v invert match (show non‑matching lines)
-o show only matching part
-q quiet mode (no output)
-A N show N lines after a match
-B N show N lines before a match
-C N show N lines around a match
-E use extended regex (egrep)
-F treat pattern as a fixed string (fgrep)Regular Expressions
Regular expressions are text patterns composed of literal and special characters, used to match strings that satisfy a given syntax. They support character classes, quantifiers, anchors, groups, and are supported by many programming languages.
Metacharacters
. matches any single character
[] matches any character in the set
[^] matches any character not in the set
[:alnum:] alphanumeric characters
[:alpha:] alphabetic characters
[:lower:] lowercase letters
[:upper:] uppercase letters
[:blank:] space and tab
[:space:] any whitespace
\b word boundary
\w word characters
\W non‑word characters
\s whitespace
\S non‑whitespace
* matches preceding element zero or more times (greedy)
+ matches preceding element one or more times
? matches preceding element zero or one time
{n} exactly n times
{n,} at least n times
{n,m} between n and m times
^ start of line
$ end of linesed
sed is a stream editor that reads input line by line, applies editing commands, and outputs the result.
Syntax
sed [options] 'script' file
Common Options
-n suppress automatic printing
-e add script commands
-f read script from file
-r, -E use extended regular expressions
-i.bak edit files in place with backupAddressing
Commands can be applied to specific lines, line ranges, or patterns.
Examples
sed -n '2,5p' file # print lines 2 to 5
sed -n '/^start/,/^end/p' file # print from line matching start to line matching end
sed -n '1~2p' file # print odd-numbered lines
sed -n '2~2p' file # print even-numbered linesCommands
p print pattern space
d delete pattern space
a\text append text after a line
i\text insert text before a line
c\text replace line with text
w file write matching lines to file
r file read file and insert after matching line
= print line number
q quitawk
awk is a programming language for pattern scanning and processing. It splits input into fields and provides built‑in variables.
Usage
awk [options] 'program' fileImportant Variables
FS field separator (default space or tab)
OFS output field separator
NF number of fields in the current record
NR number of records processed so far
$0 the entire current line
$n the nth field
FILENAME name of the current input file
RS record separator (default newline)
These tools together form a powerful toolkit for searching, editing, and reporting on text data directly from the Unix command line.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
