Skip to main content

Processing Text with Bash Using Grep, Sed, and Awk

Learn how to filter and manipulate log files in Bash scripts using powerful tools like grep, sed, and awk with practical examples.

AI-written
Inewgen
30 Oct 2026Source: Dev.to3 min read (0 views)
Share
Processing Text with Bash Using Grep, Sed, and Awk

Stock photo for illustration only, not from the actual event

Font size
  • Use grep to search for specific text patterns and logs
  • Use awk to analyze structured data and extract columns
  • Use sed to search, modify, and replace stream content
  • Combine all three commands via pipelines for advanced workflows

Text processing in Bash using the grep, sed, and awk commands allows developers and system administrators to filter and manipulate input for specific tasks such as searching log files, transforming content, and extracting structured data. You can combine these utilities with loops, conditional statements, and expressions for advanced data processing. This guide walks through each individual tool before demonstrating how to combine them into functional scripts.

The process begins by creating a log.txt file using nano to store multiple lines of text, including timestamped events ranging from successful Apache Web Server startups to database connection errors on port 3306 and disk space warnings. Next, a processing.sh script is created to load the log.txt file into a LOG variable, preparing the data for subsequent manipulation using grep, sed, and awk.

The grep command is utilized to search for specific text patterns within files. By appending grep -i "ERROR" "$LOG" to the script, it scans log.txt for the term ERROR case-insensitively and displays all matching entries, such as database connectivity failures and missing configuration files like www.yml.

Following grep, the awk command analyzes structured data and inspects file patterns. Using awk '{print $1, $3}' "$LOG" extracts and prints the first and third columns of the log file, neatly displaying dates alongside their corresponding log levels such as INFO and ERROR for cleaner data visibility.

Never miss the latest news?

Subscribe to get news summaries by email - not often enough to be annoying.

โฆษณา

Combining grep, sed, and awk using pipes (|) acts like a data assembly line, where the output of one command immediately flows as the input to the next without requiring temporary files. This modularity embodies the core Unix philosophy of building small tools that do one thing exceptionally well and chaining them together.

The sed command, or Stream Editor, specializes in searching, modifying, and replacing standard input or file content. Running sed -e 's/ERROR/New ERROR/gI' -e 's/WARNING/New WARNING/gI" "$LOG" automatically updates instances of ERROR and WARNING to New Enhanced labels throughout the entire log file.

Finally, you can combine grep, sed, and awk into a single combine.sh script using pipes. This channels filtered error and warning logs through sed for text replacement and finishes with awk for structured column formatting, resulting in a clean and automated log reporting workflow.

Source: Dev.to

Comments

Leave a Comment
0/2000

Found something wrong in this article? Report an issue with this article