Advanced Text Manipulation (sed, awk)
Explore 'sed' for stream editing and 'awk' for powerful text processing, pattern scanning, and data extraction.
Advanced Text Manipulation (sed, awk) is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Stream Editing with sed
sed, short for "stream editor," is a powerful command-line utility for parsing and transforming text. It processes text streams line by line. Think of it as a non-interactive text editor.
sed reads a line, applies a command (like substitution or deletion), prints the result, and then moves to the next line. This makes it very efficient for large files or piped data.
Simple Text Substitution
The most common use of sed is for substituting text. We use the s command, followed by a delimiter (often /), the pattern to find, another delimiter, the replacement text, and a final delimiter.
Syntax: sed 's/pattern/replacement/' filename
Try replacing "apple" with "orange" in a sample file:
#!/bin/bash
# Create a sample file
echo "I like red apple." > fruits.txt
echo "My favorite fruit is apple." >> fruits.txt
echo "Another apple here." >> fruits.txt
echo "Original content:"
cat fruits.txt
echo ""
echo "---"
echo "Running sed command:"
sed 's/apple/orange/' fruits.txt
# Clean up
rm fruits.txtGlobal & Case-Insensitive
By default, sed only replaces the first occurrence of a pattern on each line. To replace all occurrences on a line, use the g (global) flag.
To perform a case-insensitive search, use the i flag. You can combine flags, like gi.
Example: Replace all "apple" (case-insensitive) with "banana".
#!/bin/bash
# Create a sample file
echo "Apple is good." > fruits.txt
echo "I eat an apple and another Apple." >> fruits.txt
echo "Apples are tasty." >> fruits.txt
echo "Original content:"
cat fruits.txt
echo ""
echo "---"
echo "Running sed command (s/apple/banana/gi):"
sed 's/apple/banana/gi' fruits.txt
# Clean up
rm fruits.txtDeleting Lines with sed
You can also use sed to delete entire lines that match a specific pattern. The d command is used for this.
Syntax: sed '/pattern/d' filename
This is useful for removing unwanted lines, like log entries containing "DEBUG" or empty lines.
Let's remove lines containing "error":
#!/bin/bash
# Create a sample file
echo "INFO: User logged in." > log.txt
echo "WARNING: Disk space low." >> log.txt
echo "ERROR: File not found." >> log.txt
echo "INFO: Operation complete." >> log.txt
echo "ERROR: Permission denied." >> log.txt
echo "Original content:"
cat log.txt
echo ""
echo "---"
echo "Running sed command (/error/d):"
sed '/error/d' log.txt
# Clean up
rm log.txtawk: Pattern Scanning Language
awk is another powerful text processing tool, often used for data extraction and reporting. While sed is a stream editor, awk is more like a programming language designed for text.
awk processes text files line by line, just like sed. However, it treats each line as a "record" and splits records into "fields" (columns) based on a separator.
By default, awk uses whitespace (spaces, tabs) as the field separator.
Accessing Data Fields
In awk, fields are referred to by $1, $2, $3, and so on. $0 represents the entire line.
You can print specific fields using the print command within curly braces {}. The syntax is: awk '{ print $FIELD_NUMBER }' filename
Let's print the first and third fields from a simple data file:
#!/bin/bash
# Create a sample file
echo "Name Age City" > data.txt
echo "Alice 30 NewYork" >> data.txt
echo "Bob 24 London" >> data.txt
echo "Charlie 35 Paris" >> data.txt
echo "Original content:"
cat data.txt
echo ""
echo "---"
echo "Running awk command (print $1, $3):"
awk '{print $1, $3}' data.txt
# Clean up
rm data.txtFiltering Lines with awk
Just like sed, awk can also filter lines based on patterns. You specify a pattern before the action block. The action will only be performed on lines that match the pattern.
Syntax: awk '/pattern/ { action }' filename
Let's find and print only the names of people from "London":
#!/bin/bash
# Create a sample file
echo "Name Age City" > data.txt
echo "Alice 30 NewYork" >> data.txt
echo "Bob 24 London" >> data.txt
echo "Charlie 35 Paris" >> data.txt
echo "David 28 London" >> data.txt
echo "Original content:"
cat data.txt
echo ""
echo "---"
echo "Running awk command (/London/ {print $1}):"
awk '/London/ {print $1}' data.txt
# Clean up
rm data.txtInitializing & Finalizing with awk
awk allows you to specify actions to be performed before processing any lines (BEGIN block) and after all lines have been processed (END block).
BEGIN blocks are great for printing headers or initializing variables. END blocks are perfect for printing summaries, totals, or footers.
Let's add a header and footer to our output:
#!/bin/bash
# Create a sample file
echo "Alice 30 NewYork" > data.txt
echo "Bob 24 London" >> data.txt
echo "Charlie 35 Paris" >> data.txt
echo "Original content:"
cat data.txt
echo ""
echo "---"
echo "Running awk command (BEGIN/END):"
awk 'BEGIN { print "--- User Data ---" } { print $1, $2 } END { print "--- End Report ---" }' data.txt
# Clean up
rm data.txtConditional Logic in awk
You can use if statements within awk's action blocks to apply logic based on field values or other conditions. This makes awk very powerful for complex data filtering and transformation.
Syntax: awk '{ if (condition) { action } }' filename
Let's print only users older than 25:
#!/bin/bash
# Create a sample file
echo "Alice 30 NewYork" > data.txt
echo "Bob 24 London" >> data.txt
echo "Charlie 35 Paris" >> data.txt
echo "Diana 27 Berlin" >> data.txt
echo "Original content:"
cat data.txt
echo ""
echo "---"
echo "Running awk command (if $2 > 25):"
awk '{ if ($2 > 25) { print $1, $2 } }' data.txt
# Clean up
rm data.txtQuick Check: sed & awk
Consider the following content in a file named names.txt:
John Doe,30,New York
Jane Smith,25,London
Peter Jones,40,Paris
Which of the following commands would output only the names (first field) of people older than 30?
Recap: sed & awk Power
You've now explored the power of sed for stream editing and awk for advanced text processing!
sedis perfect for simple substitutions, deletions, and transformations on a line-by-line basis.awkexcels at parsing structured text, working with fields, and applying conditional logic for data extraction and reporting.
These tools are indispensable for scripting, log analysis, and data manipulation in the Linux environment.
Frequently asked questions
Is the “Advanced Text Manipulation (sed, awk)” lesson free?
Yes — the full text of “Advanced Text Manipulation (sed, awk)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Advanced Text Manipulation (sed, awk)”?
Explore 'sed' for stream editing and 'awk' for powerful text processing, pattern scanning, and data extraction. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Advanced Text Manipulation (sed, awk)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Advanced Text Manipulation (sed, awk)
- Archiving and Compression (tar, gzip, unzip)
- Disk Usage & System Info (df, du, uname)
- Sorting & Deduplicating Data (sort, uniq, cut)