A Brief Introduction To Grep, Awk & Sed - Perfect Freeze!
A Brief Introduction To Grep, Awk & Sed - Perfect Freeze!
A Brief Introduction To Grep, Awk & Sed - Perfect Freeze!
A brief introduction
to grep, awk & sed
By Cee on Aug 23, 2016
grep, awk and sed are three of the most useful command-
line tools1 in *nix world. And this article will give you a brief
introduction and basic usages of these three different
commands.
grep
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 1/8
3/25/2020 A brief introduction to grep, awk & sed - Perfect Freeze!
Usage
• Typical use
• -v: Print all of the lines that DO NOT match the search
pattern.
awk
• Control flow
• Built-in variables
Variable Meaning
$0 Current line
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 3/8
3/25/2020 A brief introduction to grep, awk & sed - Perfect Freeze!
Usage
• Typical use
• Pattern matching
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 4/8
3/25/2020 A brief introduction to grep, awk & sed - Perfect Freeze!
# print 2nd column when 1st field DOES NOT contain 'test'
awk '$1 !~ /test/ { print $2 }' file.txt
# print 2nd column when this record DOES NOT contain 'test'
awk '! /test/ { print $2 }' file.txt
sed
Usage
• Print a line: p
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 5/8
3/25/2020 A brief introduction to grep, awk & sed - Perfect Freeze!
• Remove a line: d
• Substitution: s
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 6/8
3/25/2020 A brief introduction to grep, awk & sed - Perfect Freeze!
# replace all 'test' from the 2nd to the end of each line with 'tex
sed 's/test/text/2g' file.txt
# append a new line after each line that matches the regex pattern
sed '/test/a test' file.txt
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 7/8
3/25/2020 A brief introduction to grep, awk & sed - Perfect Freeze!
# replace each line that matches the regex pattern with 'text'
sed '/test/c text' file.txt
• In-place editing: -i
(Next article)
blog.cee.moe/a-brief-introduction-to-grep-awk-and-sed.html 8/8