Linux tutorial on replacing strings using sed command

Linux tutorial on replacing strings using sed command

To replace a string, we need to use the following format.

$ sed s/replacement target string/replacement string/file name

In the following we replace the string "sample.txt" written as "appleorangemelon".

$ sed s/orange/ORANGE/ sample.txt

The execution result is

appleORANGEmelon

Replace and output a string.

In addition, as shown below, you can also get the same result by connecting the sed command with "|" after the cat command.

$ cat sample.txt | sed s/apple/APPLE/

Note that the sed command only replaces the string and outputs it, but does not rewrite the actual file contents.

If you want to keep the replaced content in text, use the redirection ">".

Options used by sed command

Command options illustrate
-e Replace with the specified script
-f file Append the contents of the script file described in the specified file
-r Using extended regular expressions

Use of sed command

Replace all lines

In the format described previously, even if there are strings matching multiple replacement target strings in one line, only the first matching string is replaced.

So, to replace all matching strings, do:

$ sed -es /apple/APPLE/g sample.txt

The execution result is

APPLEorangemelonAPPLE

Replace the beginning and end of a line

$ sed -e "s/^apple/APPLE/" sample.txt
$ sed -e "s/apple\$/APPLE/" sample.txt

If you want to replace multiple substrings, you can specify multiple scripts.

$ sed -e "s/apple/APPLE/" -e "s/orange/ORANGE/" sample.txt

Deleting a row

Specify "d" to delete the specified line. For example, to delete the second line, enter "2d".

$ sed -e '2d' sample.txt

In addition, you can also delete multiple lines. The following is to delete lines 1 to 3.

$ sed -e '1,3d' sample2.txt

You may also be interested in:
  • Detailed explanation of linux sed command (recommended)
  • sed command to find the line where the keyword is located and delete the first character before it
  • Detailed explanation of the usage of Sed command and regular expression metacharacters
  • View the sed command of the system log from a certain period of time to the present
  • One shell command a day Linux text content operation series - sed command detailed explanation
  • Usage of sed command in linux
  • Summary of the use and precautions of sed command in Linux
  • Commonly used sed commands in Linux
  • Use the sed command to modify the kv configuration file in Linux
  • How to use sed command to efficiently delete specific lines of a file

<<:  Detailed explanation of MySql automatic truncation example

>>:  React event mechanism source code analysis

Recommend

Discussion on Web Imitation and Plagiarism

A few months after entering the industry in 2005, ...

VUE realizes registration and login effects

This article example shares the specific code of ...

WeChat applet implements the snake game

This article shares the specific code of the WeCh...

Vue implements tab label (label exceeds automatic scrolling)

When the created tab label exceeds the visible ar...

Detailed tutorial for installing ElasticSearch:7.8.0 cluster with docker

ElasticSearch cluster supports動態請求的方式and靜態配置文件to ...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

Based on JavaScript ES new features let and const keywords

Table of contents 1. let keyword 1.1 Basic Usage ...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...

Solution to 1045 error when navicat connects to mysql

When connecting to the local database, navicat fo...

How to Install Xrdp Server (Remote Desktop) on Ubuntu 20.04

Xrdp is an open source implementation of Microsof...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

Free tool to verify that HTML, CSS and RSS feeds are correct

One trick for dealing with this type of error is t...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

How to import and export Docker images

This article introduces the import and export of ...