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

8 minutes read

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:

1
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.

Best Linux Books to Read in 2024

1
Linux Bible

Rating is 5 out of 5

Linux Bible

2
Practical Linux Forensics: A Guide for Digital Investigators

Rating is 4.9 out of 5

Practical Linux Forensics: A Guide for Digital Investigators

3
The Linux Command Line, 2nd Edition: A Complete Introduction

Rating is 4.8 out of 5

The Linux Command Line, 2nd Edition: A Complete Introduction

4
How Linux Works, 3rd Edition: What Every Superuser Should Know

Rating is 4.7 out of 5

How Linux Works, 3rd Edition: What Every Superuser Should Know

5
CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

Rating is 4.6 out of 5

CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

6
CompTIA Linux+ Study Guide: Exam XK0-005 (Sybex Study Guide)

Rating is 4.5 out of 5

CompTIA Linux+ Study Guide: Exam XK0-005 (Sybex Study Guide)


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:

1
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:

1
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".

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To transform a string to a specific format in Bash, you can use various string manipulation techniques. Here are some common methods:Changing case: To convert a string to uppercase, use my_string=${my_string^^}. To convert a string to lowercase, use my_string=...
To run a command in the background in Bash, you can use the following syntax: command & Here, command represents the actual command you want to run. By appending an ampersand (&) to the command, it instructs Bash to run the command in the background.Ru...
To search for a string in a file using Bash, you can make use of various commands and techniques. One common approach is to use the grep command, which stands for "global regular expression print."You can use the following command syntax to search for ...
To write a basic Bash script, follow these steps:Open a text editor and create a new file with a .sh extension (e.g., script.sh).Start the script with a shebang, which tells the system to interpret the commands using Bash. Use "#!/bin/bash" at the begi...
To extract part of a URL in bash using regex, you can use the grep or sed commands along with regular expressions.Here is an example of how you can extract a specific part of a URL in bash using regex: url="https://www.example.
To check if enable-bracketed-paste is enabled or disabled in Bash, you can use the following steps:Open the terminal or command prompt on your system.Type bash and hit Enter to launch the Bash shell.Enter bind -v | grep enable-bracketed-paste and press Enter.I...