Shell Scripting / Regular Expressions in Shell

Using sed to Modify and Substitute Text

In this tutorial, you'll learn how to use sed, a stream editor, to modify and substitute text in shell scripts. We'll cover the basics of sed and demonstrate its power through pra…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers pattern matching and text manipulation using regular expressions in shell scripts.

1. Introduction

In this tutorial, you will learn how to use sed, a stream editor, for modifying and substituting text in shell scripts. The sed command is a powerful tool that manipulates and transforms text, making it an invaluable resource for scripting and programming.

Throughout this tutorial, we will:
- Understand the basic syntax of sed
- Learn how to perform a simple text substitution
- Explore more complex text manipulations

Prerequisites: Basic knowledge of Linux command line is beneficial.

2. Step-by-Step Guide

sed uses a simple syntax: sed 's/old/new/' filename. This command replaces the first occurrence of the word "old" with "new" in each line of the file "filename".

Here are some useful tips:
- To replace all occurrences in a line, add g at the end: sed 's/old/new/g' filename
- To replace on a specific line number, add the line number before s: sed '5s/old/new/' filename
- To replace within a range of lines, specify the range before s: sed '5,10s/old/new/' filename

3. Code Examples

Example 1: Simple text substitution

echo "Hello world" | sed 's/world/universe/'

This echo command outputs "Hello world", which is then piped into sed. sed replaces "world" with "universe". The output is "Hello universe".

Example 2: Replace all occurrences

echo "Hello world world" | sed 's/world/universe/g'

In this case, sed replaces both occurrences of "world" with "universe", resulting in "Hello universe universe".

Example 3: Replace on a specific line number

sed '2s/foo/bar/' filename

This replaces the first occurrence of "foo" with "bar" on the second line of the file.

4. Summary

In this tutorial, we have covered:
- The basic syntax of sed
- How to substitute text with sed
- How to perform more complex text manipulations

As next steps, you may want to learn about more advanced sed features such as pattern deletion and multi-line transformations. You can find more information in the sed documentation here.

5. Practice Exercises

Exercise 1:
Replace "apple" with "banana" in the following string:
"apple is red"

Exercise 2:
Replace the third occurrence of "cat" with "dog" in the following string:
"cat cat cat cat cat"

Exercise 3:
Replace "foo" with "bar" only on lines 10 to 20 in a given file.

Solutions:
1. echo "apple is red" | sed 's/apple/banana/' - This will output "banana is red".
2. echo "cat cat cat cat cat" | sed 's/cat/dog/3' - This will output "cat cat dog cat cat".
3. sed '10,20s/foo/bar/' filename - This will replace "foo" with "bar" only on lines 10 to 20 in the file.

Remember, practice is crucial for mastering sed. Try to incorporate it in your scripts and see the magic it can perform. Good luck!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help