Китайско-российские отношения -- одна из примечательных тем двух сессий
The basic syntax for replacing strings in a file using sed is sed 's/old string/new string/' file name, and this command only replaces the content that appears for the first time in each line; 2. Adding the g flag can achieve global replacement, the format is sed 's/old string/new string/g' file name; 3. When the replacement content contains slashes, other symbols such as # can be used as delimiters, such as sed 's#/path/to/old#/new/path#g' file.txt; 4. Multiple replacement operations can be performed in batches through the -e parameter, such as sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt; If you need to directly modify the original file, use -i in Linux and -i '' parameters in macOS.
Of course, here is a clear structure and practical article about using sed
to find and replace strings in files:
How to find and replace strings in a file with sed?
If you operate text files in a terminal on Linux or macOS, sed
is a very powerful stream editor. It can complete search and replace operations without opening files, making it ideal for automated scripts or batch processing.
The following are several common and practical methods to help you quickly get started with sed
as string replacement.
1. Basic syntax: replace the first appearance of content
The easiest way to replace it is to use the following format:
sed 's/old string/new string/' file name
This command reads the file contents and replaces only the first matching string in each line . For example:
sed 's/foo/bar/' input.txt
This will replace the first foo
that appears in each line in input.txt
with bar
, but will not modify the original file, but will only output to the terminal.
If you want to write back to the original file directly (note: this will overwrite the original file):
On macOS:
sed -i '' 's/foo/bar/' input.txt
On Linux:
sed -i 's/foo/bar/' input.txt
Tip: Only by adding the
-i
parameter can you really modify the file content, but you must be careful to use it. It is recommended to back up the file first.
2. Replace all matches: Global replacement
The above example replaces only the first match on each row. If you want to replace all matching strings in a line , you need to add the g
flag:
sed 's/old string/new string/g' file name
for example:
sed 's/foo/bar/g' input.txt
This allows you to replace all foo
in each line with bar
.
Similarly, if you want to write to the original file, remember to add the -i
parameter.
3. Use different separators to avoid conflicts
By default, s/旧/新/
/
in is a separator. But if the content you want to replace itself contains /
, there will be problems. At this time, you can change a separator, such as #
:
sed 's#/path/to/old#/new/path#g' file.txt
In this way, the writing will not cause errors due to slashes in the path.
Commonly used alternative delimiters include |
, :
etc., and it is safer to choose symbols that are not likely to appear in the target string.
4. Batch replace multiple different strings
If you need to perform multiple replacements at once, you can use the -e
parameter to overlay multiple expressions:
sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
Or write it in a more readable form:
sed ' s/foo/bar/ s/baz/qux/ ' file.txt
This is useful for cleaning up or converting text formats at once.
Basically that's it. Although there are many functions sed
, mastering these basic usages can already meet most daily needs. Just pay attention to the use of -i
and whether you want to replace it globally, everything else is not complicated.
The above is the detailed content of How to use sed to find and replace a string in a file?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When processing files under Linux systems, it is sometimes necessary to delete lines at the end of the file. This operation is very common in practical applications and can be achieved through some simple commands. This article will introduce the steps to quickly delete the line at the end of the file in Linux system, and provide specific code examples. Step 1: Check the last line of the file. Before performing the deletion operation, you first need to confirm which line is the last line of the file. You can use the tail command to view the last line of the file. The specific command is as follows: tail-n1filena

Under normal circumstances, sed reads the line to be processed into the pattern space, and the commands in the script process the line one after another until the script is executed, then the line is output, and the pattern space is empty; then repeat the process just now action, a new line in the file is read in until the file is processed completely. However, various reasons, such as the user wanting a certain command in the script to be executed under certain conditions, or wanting the pattern space to be retained for next processing, may cause sed to not follow the instructions when processing files. Carry out the normal process. At this time, sed has set up some advanced commands to meet user requirements. If you want to learn the advanced commands of sed, you must first understand the following two buffer areas: 1. Pattern space (patt

SED, also known as Stream Editor, is a very useful tool. It is used to search for a specific word or pattern and then perform some operation on that word or pattern, or in other words, transform it. In Windows, SED is also known as the Find and Replace function. SED comes natively with Ubuntu, so there's nothing to install; just start using it. In this tutorial we will show you how to use SED or Stream Editor. "S" Command The most important command in the SED or stream editor is the "s" command. The "s" stands for substitute. The syntax is as follows: /regexp/replace/flags So let’s use a file called ”file.txt&#

The best way to make batch modifications in VSCode is to use the Find and Replace feature. 1. Use "Find and Replace" in a single file: Press Ctrl H to open the panel, enter the search and replace content, and click "Replace" or "Replace All". 2. Search across multiple files: Press Ctrl Shift F to open the search tab, expand the replacement section, and select the replacement operation for a single file or entire project. 3. Use advanced options: such as case sensitivity, full word matching and regular expressions for more precise control, such as matching numbers with \d or using capture groups for complex replacements. This feature significantly improves code maintenance efficiency through fast and precise editing.

How to use sed command for log analysis and processing in Linux? Introduction: In Linux systems, log files record the running status and operation logs of the system. For system administrators, it is very important to analyze and process log files. Among them, the sed command is a very powerful text processing tool that can efficiently analyze and process log files in the Linux environment. This article will introduce how to use the sed command to analyze and process logs, and provide some commonly used sed command examples. one

1. Introduction to sed The full name of sed (streameditor) is a streaming editor. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, write conversion programs, etc. The workflow is as follows 1. Overview of sed > sed is a An online, non-interactive editor that processes content one line at a time. It is a very good tool for text processing. It can be used perfectly with regular expressions and has extraordinary functions. During processing, the currently processed line is stored in a temporary buffer, called "patternspace", and then the sed command is used to process the contents of the buffer. After the processing is completed, the contents of the buffer are sent to the screen. Then process the next line, and repeat this until the text

To find and replace text in a file using sed, it can be implemented through the basic command s/old-text/new-text/. 1. Replace the first match for each row by default, such as sed's/Hello/Hi/'example.txt; 2. Add g parameter to replace all matches, such as sed's/Hello/Hi/g'example.txt; 3. Replace specific rows by line number, such as sed'3s/old/new/'example.txt; 4. Replace multiple rows with ranges, such as sed'2, 5s/old/new/'example.txt; 5. Replace rows that meet the conditions in combination with regular expressions, such as sed'/error/

The basic syntax of using sed to replace strings in a file is sed's/old string/new string/'file name, and this command only replaces the content that appears for the first time in each line; 2. Adding the g flag can achieve global replacement, the format is sed's/old string/new string/g' file name; 3. When the replacement content contains slashes, other symbols such as # can be used as delimiters, such as sed's#/path/to/old#/new/path#g'file.txt; 4. Through the -e parameter, multiple replacement operations can be performed in batches, such as sed-e's/foo/bar/'-e's/baz/qux/'file.txt; If you need to directly modify the original file, use -i and macOS on Linux.
