国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Replace the first match in the file
Replace all matches in a row
Replace content of a specific line
Notes and some small details
Home System Tutorial LINUX How to use sed to find and replace text in a file?

How to use sed to find and replace text in a file?

Jun 13, 2025 am 12:33 AM
sed text replacement

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 per line 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 lines by line number, such as sed '3s/old/new/' example.txt; 4. Replace multiple lines 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/s/old/new/' example.txt; 6. Use the -i parameter to modify the file directly, but be careful; 7. Use other symbols to delimit special characters, such as sed 's#/usr/local/bin#/opt/bin#' example.txt; 8. When replacing content with variables, wrap the expression in double quotes, such as sed "s/$old/$new/" fruit.txt.

How to use sed to find and replace text in a file?

Of course, here is an article on how to find and replace text in a file using sed :


It is not actually complicated to use sed to find and replace text in a file. Its basic usage is to complete the replacement operation through expressions such as s/old-text/new-text/ . For example, if you have a text file, there is a line of content that says Hello world . If you want to change it to Hello there , you can use sed to implement it quickly.

Replace the first match in the file

By default, sed will only replace the first matching string in each line.
The command format is as follows:

 sed 's/old content/new content/' file name

For example, if you run:

 sed 's/Hello/Hi/' example.txt

It will only replace the first Hello in each line to Hi , the output will not change the original file, it will only be printed on the terminal. If you want to save the result back to the file, you can add a redirect:

 sed 's/Hello/Hi/' example.txt > temp.txt && mv temp.txt example.txt

Or directly modify the original file in a system that supports -i parameters (such as Linux):

 sed -i 's/Hello/Hi/' example.txt

Replace all matches in a row

If you want to replace all the strings that appear in a certain line, instead of just replacing the first one, you can add g (the meaning of global):

 sed 's/Hello/Hi/g' example.txt

This will replace all Hello in each row with Hi .

Replace content of a specific line

Sometimes we just want to replace a specific line, not the entire file. At this time, you can use line numbers to limit the range. For example:

 sed '3s/old/new/' example.txt

This command will only replace the first old on line 3 to new .

If you want to replace a line in a range, such as lines 2 to 5, you can write it like this:

 sed '2,5s/old/new/' example.txt

You can also combine regular expressions, such as replacing only the line containing a certain keyword:

 sed '/error/s/old/new/' example.txt

This command will be replaced in the line containing error .

Notes and some small details

  • Be careful when using the -i parameter, because it will directly modify the original file , and it is recommended to back up first.

  • If you want to replace the content with special characters (such as / ), you can use other symbols instead of the delimiter, such as # or | :

     sed 's#/usr/local/bin#/opt/bin#' example.txt
  • If there are variables in the replacement content, remember to wrap the expression in double quotes to facilitate inserting shell variables:

     old="apple"
    new="orange"
    sed "s/$old/$new/" fruit.txt

    Basically that's it. By mastering these common methods, you can meet the text replacement needs in most scenarios.

    The above is the detailed content of How to use sed to find and replace text in a file?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly delete the line at the end of a file in Linux How to quickly delete the line at the end of a file in Linux Mar 01, 2024 pm 09:36 PM

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

Comprehensive summary: Detailed explanation of Linux sed multi-line processing Comprehensive summary: Detailed explanation of Linux sed multi-line processing Jan 05, 2024 pm 04:11 PM

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

Stream Editor (SED): Basics Stream Editor (SED): Basics Mar 20, 2024 pm 03:11 PM

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&#

How to use sed command for log analysis and processing in Linux? How to use sed command for log analysis and processing in Linux? Jul 28, 2023 pm 11:53 PM

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

How to use the sed command in linux How to use the sed command in linux May 18, 2023 pm 10:07 PM

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

PHP Programming Guide: Master how to replace punctuation in text with PHP PHP Programming Guide: Master how to replace punctuation in text with PHP Mar 26, 2024 pm 01:03 PM

PHP Programming Guide: Master how to replace punctuation marks in text with PHP. As a popular server-side scripting language, PHP is widely used in the field of web development. When processing text data, it often involves the need to replace punctuation marks in the text. This article will introduce how to use PHP to implement this function and give specific code examples. What are punctuation marks? Punctuation marks are symbols used to assist language expression, convey semantics, or construct text structure. Common punctuation marks include periods, commas, question marks, and exclamation marks.

C program: Replace a word in text with another given word C program: Replace a word in text with another given word Sep 08, 2023 pm 10:37 PM

In this program, we are given three strings txt, oldword and newword. Our task is to create a C program that replaces a word in a text with another given word. The program will search for all occurrences of oldword in the text and replace them with newword. Let’s take an example to understand this question – enter text="Iamlearningprogramming" oldword="learning" newword="practicing&rdqu

Proficient in PHP text replacement, advanced technology! Proficient in PHP text replacement, advanced technology! Mar 28, 2024 am 10:12 AM

Proficient in PHP text replacement, advanced technology! In website development and data processing, text replacement is a common task, and PHP, as a popular server-side scripting language, has powerful text manipulation functions. This article will introduce the technical advancement of how to implement text replacement in PHP, including common replacement methods and some advanced techniques. Basic text replacement First, let us review the basic text replacement function str_replace() in PHP. This function simply replaces the specified content in the string with a new one

See all articles