Mastering the Text Trio: grep, sed, and awk for Powerful Linux Text Processing
This article provides a comprehensive guide to the three essential Unix text‑processing tools—grep, sed, and awk—explaining their concepts, syntax, key options, regular‑expression fundamentals, and practical command examples for efficient file manipulation.
Text Trio: grep, sed, 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
-o show only matching part
-q quiet mode
-A N after‑context
-B N before‑context
-C N context
-E use extended regular expressions (egrep)
-F fixed strings (fgrep)Regular Expressions
Regular expressions are patterns composed of literal and special characters used to match strings. They include metacharacters, character classes, quantifiers, anchors, groups, and backreferences, and are supported by many programming languages.
Metacharacters
. matches any single character
[] character class
[^] negated class
[:alnum:], [:alpha:], [:lower:], [:upper:], [:blank:], [:space:], etc.
\w word character, \W non‑word, \s whitespace, \S non‑whitespace
Quantifiers
* 0 or more (greedy)
+ 1 or more
? 0 or 1
{n} exactly n
{m,n} between m and n
{n,} at least n
Anchors
^ start of line
$ end of line
\b word boundary
\< and \> word start/end (POSIX)
Groups
Parentheses ( ) group patterns; backreferences \1, \2 refer to captured groups.
sed
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 prefixed by an address or range (line numbers, regex, etc.). Examples:
1,5p # print lines 1 to 5
/START/,/END/p # print from line matching START to line matching END
3~2p # print every 2nd line starting at line 3Commands
p print pattern space
d delete matching lines
a\text append after line
i\text insert before line
c\text replace line
w file write matching lines to file
r file read file into output
= print line number
! apply command to non‑matching lines
q quit
awk
awk is a pattern‑action language for processing and reporting text, often used for column‑wise operations.
Usage
awk [options] 'program' fileImportant Variables
FS field separator (default whitespace)
OFS output field separator
NF number of fields in current record
NR record number
$0 entire current line
$n nth field
FILENAME name of current file
RS record separator (default newline)
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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
