0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Advanced Text Manipulation (sed, awk)

Explore 'sed' for stream editing and 'awk' for powerful text processing, pattern scanning, and data extraction.

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.txt

All lessons in this course

  1. Advanced Text Manipulation (sed, awk)
  2. Archiving and Compression (tar, gzip, unzip)
  3. Disk Usage & System Info (df, du, uname)
  4. Sorting & Deduplicating Data (sort, uniq, cut)
← Back to Linux Command Line & Bash Scripting Mastery