---
title: "Using these sed Examples to Power Up Your Command Line Skills"
description: "Learn to use practical sed examples via this command line utility for more powerful shell scripting in this ATA Learning tutorial!"
canonical: "https://adamtheautomator.com/sed-examples/"
---

# Using these sed Examples to Power Up Your Command Line Skills

> Learn to use practical sed examples via this command line utility for more powerful shell scripting in this ATA Learning tutorial!

Source: https://adamtheautomator.com/sed-examples/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Power Up Your Command Line Skills with These sed Examples](https://adamtheautomator.com/wp-content/uploads/2023/01/sed-examples.jpg)

# Power Up Your Command Line Skills with These sed Examples

[![](https://secure.gravatar.com/avatar/1bc5e1d52466fc3739f550c8d78be310684747bf1466a98d698320627ea243e2?s=192&d=mm&r=g)Michael Nguyen Tu](https://adamtheautomator.com/author/michael-nguyen-tu/)10 January 20238 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing sed on Ubuntu](#installing-sed-on-ubuntu)
*   [Replacing Strings in a Text File](#replacing-strings-in-a-text-file)
*   [Replacing the Nth Occurrences of a String](#replacing-the-nth-occurrences-of-a-string)
*   [Replacing All Occurrences of a Matching Pattern/String](#replacing-all-occurrences-of-a-matching-patternstring)
*   [Replacing a Matching Pattern String on a Specific Line](#replacinga-matching-pattern-stringon-a-specific-line)
*   [Outputting Changed Lines of Strings Exclusively](#outputting-changed-lines-of-strings-exclusively)
*   [Conclusion](#conclusion)

Have you been looking for another way to perform editing operations on texts? Look no further! This tutorial has got you covered with stream editor sed examples.

In a nutshell, sed allows you to perform a wide range of edits on files and text streams in a non-interactive way. You make the editing decisions as you call the commands. And in this tutorial, you will learn how to use sed commands and effectively streamline your workflow.

Read on and power up your command line skills!

## **Prerequisites**

Ensure you have the following to follow along with the hands-on demonstration in this tutorial:

*   A command line interface (e.g., Terminal on macOS or Linux) – This tutorial uses Ubuntu Linux 20.04, but the commands should work on most popular operating systems.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A text editor (e.g., nano, vi, or [Sublime](https://adamtheautomator.com/sublime-text-on-ubuntu/) Text) – This tutorial uses Nano.

Related:[Essential Tips for Installing & Using Sublime Text on Ubuntu](https://adamtheautomator.com/sublime-text-on-ubuntu/)

## Installing sed on Ubuntu

sed is generally included as a standard utility in most Unix-like operating systems, including Linux and macOS. But regardless, double-checking if you already have sed on your system would be okay and is a basic procedure before performing text transformations.

1\. Open your terminal, and run the below `sed` command to check which sed version is installed on your system.

```bash
sed --version
```

If sed is installed, you will see the sed version number and some information about the build, as shown below.

![Verifying sed’s installed version ](https://adamtheautomator.com/wp-content/uploads/2023/01/image-139.png)

Verifying sed’s installed version

Otherwise, you will see the error message below if sed is not installed.

This error message indicates that the `sed` command was not found in your system’s PATH environment variable.

![Getting an error running sed commands](https://adamtheautomator.com/wp-content/uploads/2023/01/image-140.png)

Getting an error running sed commands

2\. Next, run the `[[apt-get update](https://www.cyberciti.biz/faq/what-does-sudo-apt-get-update-command-do-on-ubuntu-debian/)` below command to update and install `sed` on your system.

This command fetches the latest package information from the configured package sources, including the package’s dependencies, files, and scripts. Running this command is necessary to ensure you install the latest version of sed and any other packages you may need.

Related:[Learning Ubuntu Apt Get Through Examples](https://adamtheautomator.com/ubuntu-apt-get/)

```bash
sudo apt-get update -y && sudo apt-get install sed -y
```

![Installing sed on the operating system](https://adamtheautomator.com/wp-content/uploads/2023/01/image-141.png)

Installing sed on the operating system

3\. Lastly, run the following command to verify the `sed` command is installed on your system.

This command prints the full path to the sed executable, which in this example is _/usr/bin/sed_.

Once you have installed sed and verified it is in your PATH, you can use the **`sed`** command to perform various text transformations and manipulations.

```bash
which sed
```

![Verifying that the sed command is installed](https://adamtheautomator.com/wp-content/uploads/2023/01/image-142.png)

Verifying that the sed command is installed

## Replacing Strings in a Text File

sed is a popular stream editor available on most Unix-like operating systems, and one of sed’s most handy features is replacing strings in a text file.

Below are some scenarios, but are not limited to, where replacing strings is helpful:

*   Updating a list of URLs to reflect a new domain name.
*   Changing product IDs in a database or CSV file.
*   Formatting data for import into another system and many more.

The basic syntax for using sed is as follows where:

| Placeholder | Function |
| --- | --- |
| command | Controls sed’s behavior. |
| options | Allows you to customize the behavior of the sed command |
| input\_file | Specifies the file or text stream that you want to edit. Typically, you can specify a single file or stream, but concatenating multiple files or streams by redirection also works. |
| output\_file | Specifies the file that contains the results of your sed command. The file can be new or the same as input\_file, depending on how you use sed. |

```bash
sed command options input_file | output_file
```

Suppose you have a text file called `urls.txt` that contains a list of URLs and want to update all of the URLs to use a new domain name.

1\. Run the `echo` commands below to create the `urls.txt` file in the current directory. These commands do not provide output but write specified texts to the file.

```bash
echo "http://old-domain.com/page1" > urls.txt && echo "http://old-domain.com/page2" >> urls.txt && echo "http://old-domain.com/page3" >> urls.txt
```

2\. Next, run the `cat` command to view the contents of the _urls.txt_ file. `cat urls.txt`

```bash
cat urls.txt
```

The output below confirms the three old URLs written in the _urls.txt_ file.

![Viewing the contents of the urls.txt file](https://adamtheautomator.com/wp-content/uploads/2023/01/image-143.png)

Viewing the contents of the _urls.txt_ file

3\. Now, run the command below to find and substitute (`s`) all occurrences of the string `old-domain.com` with _`new-domain.com`_ in the `urls.txt` file.

```bash
sed 's/old-domain.com/new-domain.com/' urls.txt
```

You will see that the URLs in your input file have been updated with the new domain, as shown.

![Replacing a string in a text file](https://adamtheautomator.com/wp-content/uploads/2023/01/image-144.png)

Replacing a string in a text file

## Replacing the Nth Occurrences of a String

In addition to the basic search-and-replace functionality, sed has additional built-in commands that allow you to perform more complex text transformations. One such case is replacing the nth occurrence of the pattern string in a single line of text.

For example, you wish to update the second field in a CSV file with a new value or update the third occurrence of a string in a database dump.

The basic syntax for this command is as follows where:

*   `pattern` – is the string to replace.
*   `replacement` – is the string to use as a replacement.
*   `n` – is the nth occurrence of the string to replace (e.g., `1`\=first occurrence, `2`\=second occurrence…).

```bash
sed 's/pattern/replacement/n' input_file
```

To see how to replace the nth occurrence of a string, follow these steps:

Run the following `echo` command to create a line of text with three occurrences of the pattern `quick`. The `sed` command then replaces (`s`) the second occurrence (`2`) of the pattern `quick` with the string `fast`.

```bash
echo "The quick and quick and quick brown fox jumps over the lazy dog" | sed 's/quick/fast/2'
```

As you can see below, only the second occurrence of the pattern **quick** has been replaced with the string **fast**, while all other occurrences remain unchanged.

The `nth` command is case-sensitive by default. As a result, the command only looks for the exact match of the pattern. To ignore the letter case of the pattern, jump to the following step.

![Replacing the nth occurrence of a string](https://adamtheautomator.com/wp-content/uploads/2023/01/image-145.png)

Replacing the nth occurrence of a string

> 💡 _Note that the **`nth`** command counts occurrences of the pattern from left to right. Moreover, the **`nth`** command only works on a single line of text. To perform a substitution on multiple lines, use a different method demonstrated in the following section._

Next, run the below command to replace the `quick` pattern’s second (`2`) occurrence with `fast` while ignoring (`i`) the letter case.

The `i` option comes in handy when working with text with inconsistent capitalization.

```bash
echo "The Quick and QuiCk and Quick brown fox jumps over the lazy dog" | sed 's/quick/fast/2i'
```

In the output below, the second occurrence of the word quick (**QuiCk**) is successfully replaced with the string **fast** — regardless of the letter case.

![Replacing the nth occurrence of a string while ignoring the letter case](https://adamtheautomator.com/wp-content/uploads/2023/01/image-146.png)

Replacing the nth occurrence of a string while ignoring the letter case

## Replacing All Occurrences of a Matching Pattern/String

Instead of replacing the nth occurrence of a string, perhaps you plan to replace all its occurrences. Doing so dramatically helps with your plan to mass-update every occurrence of a Copyright string with the current year, for example.

1\. Create a file called _input.txt_ with the following contents in your preferred text editor.

```
This is a quick test.
The quick brown fox jumps over the lazy dog.
Quick, you need to finish this task before it gets too late.
```

2\. Next, run the following command to replace (`s`) all occurrences of the word `quick` with `fast` from the `input.txt` file. Once replaced, the changes are written to the `output.txt` file.

```bash
sed 's/quick/fast/g' input.txt > output.txt
```

3\. Now, run the command below to view the `output.txt` file’s contents and verify if the replacement has been made.

```bash
cat output.txt
```

You will see all occurrences of the word **quick** have been replaced with **fast**, as shown below. Similar to the `nth` command, the `g` command is case-sensitive and replaces only the exact matches.

The **Quick** string will not get replaced, as shown below, but you can use the `i` option to perform case-insensitive substitutions.

![Replacing All the Occurrences of a Matching Pattern/String](https://adamtheautomator.com/wp-content/uploads/2023/01/image-147.png)

Replacing All the Occurrences of a Matching Pattern/String

## Replacing a Matching Pattern String on a Specific Line

Perhaps you already know which line of the string you want to replace lies. Why replace all occurrences if you can replace strings on specific lines? This feature is useful when making changes to a particular line in a file or stream but leaving the other lines untouched.

Below is the syntax for replacing a string from a specific line, where `n` represents the line number.

```bash
sed 'n s/pattern/replacement/' input_file output_file
```

1\. Create a file called _myfile.txt_ with the following content.

```
test1
test2
test3
```

2\. Next, run the below `sed` command to perform the following:

*   Search for the word `test2` from the second line (`2`) of the `myfile.txt` file.Replace (`s`) the word `test2` with `test2_changed`.Write the changes (`>`) to a new file called `myfile_changed.txt`.

This command does not provide output to the terminal since the changes are written to a new file. But you will verify the changes in the following step.

```bash
sed '2 s/test2/test2_changed/' myfile.txt > myfile_changed.txt
```

3\. Lastly, run the below cat command to view the contents of the `myfile_changed.txt` file.

```bash
cat myfile_changed.txt
```

As you can see below, the second line of the _myfile.txt_ file has been replaced with _**test2\_changed**_, while all other lines remain unchanged.

![sed examples - Verifying the changed string on a specific line](https://adamtheautomator.com/wp-content/uploads/2023/01/image-148.png)

Verifying the changed string on a specific line

## Outputting Changed Lines of Strings Exclusively

Instead of printing the entire content of a file after making changes, you may want to print the changed lines exclusively. How? By using the `p` flag together with the `-n` option.

Below is the syntax in printing changed lines of strings exclusively where:

*   The `-n` option tells `sed` to suppress the default behavior of printing every line.
*   The `p` flag tells `sed` to print the output of the command.

```bash
sed -n 's/pattern/replacement/p' input_file output_file
```

Open your _myfile.txt_ file and replace the content with the following. Notice that the second line is incorrect since the word `first` should be `second` instead.

```
This is the first line of my file.
This is the first line of my file.
This is the third line of my file.
```

Now, run the below `sed` command to replace the string `first` with `second` in the second line (`2`) of the `myfile.txt` but only output the modified lines (`p`).

```bash
sed -n '2 s/first/second/p' myfile.txt
```

You will see the output of the modified second line only on your terminal, as shown below.

![Outputting changed lines exclusively](https://adamtheautomator.com/wp-content/uploads/2023/01/image-149.png)

Outputting changed lines exclusively

## Conclusion

In this tutorial, you have used different sed usage examples to perform search-and-replace operations. Moreover, you touched on more advanced techniques, such as replacing specific occurrences and outputting only the modified lines.

sed is a powerful command line tool that lets you perform various text manipulation tasks. Whether making global replacements across an entire file or targeted replacements on specific lines, sed will do the trick.

Now, why not explore more about [regular expressions](https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html)? Regular expressions are essential to many text manipulation tools, including sed. These expressions are a time-saver and lessen exerted efforts. When? While you perform complex searches and replace operations on large amounts of text.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fsed-examples%2F&text=Power%20Up%20Your%20Command%20Line%20Skills%20with%20These%20sed%20Examples)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fsed-examples%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fsed-examples%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26989-monitor-windows-servers-prometheus-grafana-codex.webp)

### [How to Monitor Windows Servers with Prometheus and Grafana](/monitor-windows-servers-prometheus-grafana/)

Set up Windows Server monitoring with windows\_exporter, Prometheus, Grafana, and Alertmanager. Learn how to secure port 9182, scrape targets, and validate alerts.

![](https://adamtheautomator.com/wp-content/uploads/2026/05/featured_image-11.webp)

### [How to Troubleshoot Active Directory Replication Errors](/troubleshoot-active-directory-replication-errors/)

Troubleshoot Active Directory replication errors by isolating 1311, 1722, 2087, and USN rollback issues with repadmin, dcdiag, DNS, RPC, and KCC checks.

![](https://adamtheautomator.com/wp-content/uploads/2026/04/featured_image-6.webp)

### [How to Survive the 2026 Secure Boot Certificate Expiry](/survive-2026-secure-boot-certificate-expiry-3/)

Deploy Windows UEFI CA 2023 before the June 2026 certificate expiry. Inventory devices, update OEM firmware, and trigger enrollment via Intune or PowerShell registry settings.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
