Skip to main content
Terminal Tips

10 sed One-Liners Every Linux Admin Should Know (With Real Example)

In this article, we explore 10 beginner-friendly sed one-liners that show how to search, replace, and manipulate text directly from your terminal.

Ravi Saive

If you’ve worked with Linux long enough, you’ve probably used sed, which is short for Stream Editor, and it can search, find, replace, delete, and edit text file all in one go, and right from your terminal.

In this article, I’ll show you 10 must-know sed one-liners that will help you clean up files, tweak configs, or automate boring text edits. If you’re a beginner, don’t worry, I’ll walk you through it with a real example you can run on your machine.

Real Example: Let’s Create a Test File

We'll create a small, realistic sample file that mimics the kind of text you might find in configuration files, logs, or simple reports, which will allow you to run the sed commands yourself and actually see how they change the content.

Open your terminal and run the command below:

cat <<EOF > sample.txt
Welcome to Tecmint
This is a test file
The quick brown fox
jumps over the lazy dog

Foo bar foo
Pattern is here
Whitespace at end    
   Leading whitespace
EOF

This command uses a here-document (<<EOF) to insert multiple lines of text directly into a new file called sample.txt. Once you press Enter, it will write all the text lines into the file until it sees EOF at the end.

Now, if you open the file with cat sample.txt or less sample.txt, you’ll see a few different types of content: normal text, a blank line, some repeated words (foo and bar), a line with trailing spaces, and another with leading spaces.

These different elements are intentional, we’ll use them to demonstrate how sed can manipulate real-world text.

cat sample.txt
or
less sample.txt
Create a Test File

1. Replace a Word in a File (In-Place)

One of the most common and practical uses of sed is to search for a specific word or pattern and replace it with another.

Let’s say you have a file where the word foo appears multiple times, and you want to change every instance to uppercase FOO. You can do this easily with the following command:

sed -i 's/foo/FOO/g' sample.txt

If you’re editing important files, it’s a good habit to create a backup before making changes. You can do this by adding a backup extension with the -i flag like so:

sed -i.bak 's/foo/FOO/g' sample.txt

This will edit sample.txt and also create a backup file called sample.txt.bak, which contains the original content in case something goes wrong.

2. Delete Empty Lines

When working with configuration files, logs, or scripts, you'll often come across unnecessary blank lines, which don’t affect functionality, but they do make files look messy and harder to scan, especially when you’re troubleshooting or reviewing large outputs.

The following one-liner deletes all blank lines from a file:

sed '/^$/d' sample.txt

You can also redirect the result to a new file like this:

sed '/^$/d' sample.txt > cleaned.txt

This way, your original file stays untouched, and the cleaned version is saved separately.

3. Print Specific Lines

Sometimes, you don’t need to process an entire file, you just want to view a specific range of lines. Maybe you're debugging a script, reviewing a config, or pulling a snippet from a log file.

To print only certain lines, let’s say lines 2 through 4, you can use the following one-liner:

sed -n '2,4p' sample.txt

If you're working with the sample.txt file we created earlier, running this command would give you the following output:

This is a test file
The quick brown fox
jumps over the lazy dog

4. Delete a Specific Line

Sometimes, you just need to remove a specific line from a file, maybe it's a typo, a broken config, or just junk you want gone.

Let’s say you have a file called sample.txt, and line 3 contains the text:

The quick brown fox

To delete this exact line, run the following command:

sed '3d' sample.txt

By default, sed will print the edited result to the terminal, but won’t change the file unless you add the -i flag (we'll skip that for now so you can see the output safely).

1. Welcome to Tecmint
2. This is a test file
3. jumps over the lazy dog

If you want to edit the file directly, just add -i:

sed -i '3d' sample.txt

5. Replace Only the First Occurrence Per Line

When working with text files, you’ll often want to replace a word or phrase, but not every instance, maybe just the first one on each line.

sed 's/bar/BAR/' sample.txt

Only the first "bar" is changed to uppercase "BAR". If there were more "bar"s on the same line, they would be left as-is.

If you do want to replace every occurrence of "bar" on each line, just add the g flag at the end like so:

sed 's/bar/BAR/g' sample.txt

6. Insert a Line Before a Pattern

Sometimes, you need to add a line of text before another line that matches a certain pattern. For example, if you're editing a configuration file or a script, and you want to insert a comment or directive just before a specific line.

sed '/Pattern/i Before this line' sample.txt

In this command:

  • /Pattern/ is the search term - sed looks for any line that contains the word "Pattern".
  • i stands for insert.
  • The text "Before this line" will be inserted just before the matched line.

7. Append a Line After a Pattern

One of the cool things you can do with sed is insert new text automatically after a specific line, especially when you’re processing config files, logs, or large output files.

The a command in sed stands for append, and it adds a new line after every line that matches a given pattern.

sed '/Pattern/a After this line' sample.txt

So, when this command runs, sed scans each line of sample.txt. Whenever it finds a line that matches the word "Pattern", it doesn’t change the original line instead, it inserts a brand-new line right after it with the content you provided.

8. Replace Whole Line Matching a Pattern

Sometimes, instead of just changing a word or part of a line, you want to completely replace an entire line, but only if it contains a specific keyword or pattern.

sed '/Whitespace/c\This line was replaced' sample.txt

Let’s break it down:

  • /Whitespace/ tells sed to look for any line that contains the word “Whitespace” (case-sensitive).
  • c\This line was replaced tells it to replace the entire matching line with the exact string "This line was replaced".

So, wherever sed finds "Whitespace" in the line - whether it’s "Whitespace at end" or " Leading whitespace" - it wipes out the full line and inserts your new content in its place.

9. Replace in Multiple Files at Once

Let’s say you're working on a website, and you've got dozens, maybe hundreds of .html files where old links still use http:// instead of the secure https://. Manually opening each file to make this change would be painfully slow.

sed -i 's/http:/https:/g' *.html

Here’s what’s happening: the -i flag tells sed to edit files in-place (no need to redirect the output), and 's/http:/https:/g' is the substitution command, which finds every instance of http: and replaces it with https:.

The g at the end ensures it changes all matches on each line. The wildcard *.html targets all files in the current directory ending with .html.

This is extremely useful when migrating a site to HTTPS or cleaning up code for better security - don’t run this on live data without a backup.

The -i flag makes permanent changes, and there's no "undo" unless you've got version control or a backup. A safer approach is to create backups automatically like this:

sed -i.bak 's/http:/https:/g' *.html

10. Remove Leading and Trailing Whitespace

One of the most common formatting issues in text files, especially when working with logs, config files, or data exports is the presence of unwanted spaces or tabs at the beginning or end of lines.

These may not seem harmful, but they can cause issues in scripting, parsing, and even visual alignment. Thankfully, sed makes it easy to clean this up.

sed 's/^[ \t]*//;s/[ \t]*$//' sample.txt

Let’s break it down:

  • s/^[ \t]*// - This part removes all leading whitespace (spaces or tabs). ^ matches the start of a line, and [ \t]* means zero or more spaces or tabs.
  • s/[ \t]*$// - This part removes all trailing whitespace. $ anchors the pattern to the end of the line.

The semicolon (;) lets us run both substitutions in a single sed command.

Final Thoughts

You just walked through 10 practical sed one-liners with real examples you can actually try on your own. These aren’t just cool tricks, they’re real tools you’ll reach for again and again when editing config files, cleaning up logs, or automating those boring, repetitive text edits.

🗨️ Got Questions?

If you’re stuck or want to share your own sed trick, leave a comment below. And if you want more guides like this for awk, grep, or find, let me know - I’ve got tons of one-liners coming your way.

Keep Learning with TecMint Pro

Want more than just quick tips? With TecMint Pro, you’ll unlock ad-free reading, premium content, downloadable resources, certification resources, and our weekly newsletter - all in one place.

Plan Regular Price Limited-Time Price What You Get
Sudo $75 / year $50 / year Get unlimited yearly access to all premium content and instant downloads of 8 Linux eBooks ($200 value).
Root $129 lifetime $99 one-time Get lifetime access and all 8 premium Linux eBooks ($200 value) with free lifetime updates.
🚨 Hurry! This offer is only available for a limited time, and only the first 100 members will get lifetime access at this discounted rate. After that, regular pricing returns.