Skip to main content
ubuntuask.com

Back to all posts

How to Use the "Sed" Command For String Manipulation In Bash?

Published on
5 min read
How to Use the "Sed" Command For String Manipulation In Bash? image

Best Bash String Manipulation Tools to Buy in October 2025

1 Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

BUY & SAVE
$23.74 $39.99
Save 41%
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali
2 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • QUALITY ASSURANCE: EVERY BOOK IS VETTED FOR GOOD CONDITION.
  • AFFORDABLE PRICES: SAVE MONEY WITH QUALITY USED BOOKS!
  • ECO-FRIENDLY CHOICE: SUPPORT RECYCLING BY BUYING USED.
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
3 Shell Scripting: Expert Recipes for Linux, Bash, and more

Shell Scripting: Expert Recipes for Linux, Bash, and more

BUY & SAVE
$38.49 $49.99
Save 23%
Shell Scripting: Expert Recipes for Linux, Bash, and more
4 Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting

Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting

BUY & SAVE
$37.98
Command Line Fundamentals: Learn to use the Unix command-line tools and Bash shell scripting
5 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$25.73 $49.99
Save 49%
Classic Shell Scripting
6 Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

BUY & SAVE
$2.99
Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming
7 BASH Guide

BASH Guide

BUY & SAVE
$0.99
BASH Guide
8 Mastering Shell for DevOps: Automate, streamline, and secure DevOps workflows with modern shell scripting

Mastering Shell for DevOps: Automate, streamline, and secure DevOps workflows with modern shell scripting

BUY & SAVE
$33.99
Mastering Shell for DevOps: Automate, streamline, and secure DevOps workflows with modern shell scripting
9 The Official Linux Mint 22 Handbook: Your Essential Companion (2025 Edition) (Mastering Linux Mint 22: The Complete Guide Series)

The Official Linux Mint 22 Handbook: Your Essential Companion (2025 Edition) (Mastering Linux Mint 22: The Complete Guide Series)

BUY & SAVE
$22.00
The Official Linux Mint 22 Handbook: Your Essential Companion (2025 Edition) (Mastering Linux Mint 22: The Complete Guide Series)
10 LINUX FOR HACKERS: LEARN CYBERSECURITY PRINCIPLES WITH SHELL,PYTHON,BASH PROGRAMMING USING KALI LINUX TOOLS. A COMPLETE GUIDE FOR BEGINNERS (HACKERS ESSENTIALS)

LINUX FOR HACKERS: LEARN CYBERSECURITY PRINCIPLES WITH SHELL,PYTHON,BASH PROGRAMMING USING KALI LINUX TOOLS. A COMPLETE GUIDE FOR BEGINNERS (HACKERS ESSENTIALS)

BUY & SAVE
$18.50
LINUX FOR HACKERS: LEARN CYBERSECURITY PRINCIPLES WITH SHELL,PYTHON,BASH PROGRAMMING USING KALI LINUX TOOLS. A COMPLETE GUIDE FOR BEGINNERS (HACKERS ESSENTIALS)
+
ONE MORE?

The sed command, short for stream editor, is a powerful tool for performing string manipulation and text transformations in the Bash shell. It operates on a line-by-line basis and allows you to find and replace text, delete lines, insert or append text, and perform other modifications to input streams.

To use sed for string manipulation in Bash, you typically provide it with a pattern and an action to be performed on that pattern. The simplest example is using sed to find and replace text in a file or input stream:

sed 's/pattern/replacement/' input.txt

In the above command, s denotes the substitution command, pattern represents the text you want to find (can be a regular expression), and replacement is the text you want to replace it with. input.txt is the file you want to process, but you can also use sed without specifying a file to read from standard input.

Here are a few more common sed commands for string manipulation:

  • Deleting lines: sed '/pattern/d' input.txt deletes all lines that match the given pattern.
  • Inserting or appending text: sed '1 i\New line' input.txt inserts a line of text at the beginning, and sed '$ a\New line' input.txt appends a line at the end.
  • Replacing text within a specified range: sed '2,4 s/pattern/replacement/' input.txt replaces text only within lines 2 to 4.
  • Executing multiple commands: sed -e 'command1' -e 'command2' input.txt allows you to execute multiple sed commands sequentially.

sed also offers many advanced features, including regular expressions, backreferences, matching groups, and more. You can refer to the sed manual (man sed) for extensive details and examples.

Overall, the sed command is a versatile tool for manipulating strings and performing various text transformations within the Bash shell. It can be highly useful for tasks like text processing, data manipulation, and automated editing of files.

How to perform case-insensitive search and replace using "sed"?

To perform case-insensitive search and replace using "sed", you can use the -i flag along with the s command. Here is the syntax:

sed -i 's/search/replace/gI' file.txt

Here, search is the text you want to search for, replace is the text you want to replace it with, and file.txt is the path to the file you want to perform the search and replace on.

The g flag is used to perform the replacement globally (i.e., on all occurrences of the search term), and the I flag is used to perform case-insensitive search.

If you want to perform the search and replace without changing the original file, you can omit the -i flag:

sed 's/search/replace/gI' file.txt > file_modified.txt

This command will create a new file called file_modified.txt with the changes applied, while leaving the original file unchanged.

How to extract specific lines from a file using "sed"?

To extract specific lines from a file using "sed", you need to specify the line numbers or a pattern to match the lines you want to extract. Here are two common methods:

  1. Extracting specific line numbers: sed -n '2p;4p;6p' filename This command will extract lines 2, 4, and 6 from the file called "filename". The -n option suppresses automatic printing, and each number followed by "p" represents the line number you want to extract.
  2. Extracting lines based on a pattern: sed -n '/pattern/p' filename This command will extract all lines from the file called "filename" that match the specified pattern. Replace "pattern" with the desired pattern, such as a specific word or expression contained within the lines you want to extract.

Make sure to replace "filename" with the actual name of the file you want to extract lines from.

How to use the "r" command to read contents from a file and insert them in "sed"?

To use the "r" command in sed to read contents from a file and insert them, you can follow these steps:

  1. Create a new sed script or use the sed command directly in the terminal.
  2. Use the "r" command in sed, followed by the file name from which you want to read the contents. sed 'r filename' input.txt Replace "filename" with the actual name of the file you want to read from.
  3. Position the "r" command within the sed script or terminal command where you want the contents of the file to be inserted. For example, if you want to insert the contents of the file after a specific line, you can use the "r" command after that line: sed '/specific_line/ r filename' input.txt Replace "specific_line" with the line number or pattern that identifies the location where you want to insert the file contents.
  4. Run the sed command, and it will read the contents from the specified file and insert them at the desired location.

Note: The "r" command inserts the contents of the file every time the pattern or line number is matched. If you want to insert the file contents only once, you can use the "w" command instead of "r".