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 replace * with #* in Bash, you can use the following command: echo "original string" | sed 's/\*/#*/g' This command uses the sed command to search for instances of * and replace them with #* in a given string. The s command in sed stands for...
To replace a string in binary files using bash, you can use the sed command with the -i option. This option allows you to perform an in-place edit of the file. You can specify the search string and the replacement string as arguments to the sed command. For ex...
To use any bash command in a bash function, you simply need to define the desired command within the function block. You can include any valid bash command or series of commands within the function. For example, you can create a function that checks for the ex...
To pass a seconds variable from bash to Perl, you can use command line arguments. In your bash script, you can call the Perl script and pass the seconds variable as an argument. For example:Bash script: #!/bin/bash seconds=60 perl script.pl $seconds Perl scri...
To move a JSON item one level higher in a bash script, you can use a combination of command-line tools like jq and sed. First, you can use jq to extract the desired item and save it to a temporary file. Then, you can use sed to remove the item from its current...
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...