How to replace strings in files in linux

青灯夜游
Release: 2022-03-24 18:12:17
Original
26482 people have browsed it

In Linux, you can use the sed command to find and replace strings in files. This command uses stream editing mode and can match and process data in text files based on regular expressions; the replacement syntax is: "sed 's/string or regular expression to search for/replacement value/g' filename to perform the operation on".

How to replace strings in files in linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

When you are working with text files, you most likely need to find and replace strings in the file. In Linux this can be done by using the sed command, which is mainly used to replace text in a file.

What is the sed command?

The sed command stands for Stream Editor, which is used to perform basic text operations on Linux. It can perform various functions such as searching, finding, modifying, inserting or deleting files.

The sed command uses stream editing mode and can perform complex regular expression matching.

The sed command matches and processes data in text files based on regular expressions.

It can be used for the following purposes:

  • Find and replace content matching a given format.

  • Find and replace content matching the given format in the specified line.

  • Find and replace all lines matching the given format.

  • Search and replace two different patterns at the same time.

sed command replaces strings in files

Common sed syntax for replacing strings.

sed -i 's/Search_String/Replacement_String/g' Input_File
Copy after login

First we need to understand sed syntax to do this. See details.

  • sed: This is a Linux command.

  • -i: This is an option of the sed command. What does it do? By default, sed prints results to standard output. When you use sed to add this option, then it will modify the file in place. When you add a suffix (for example, -i.bak), a backup of the original file is created.

  • s: The letter s is a substitution command.

  • Search_String: Search for a given string or regular expression.

  • Replacement_String: Replacement string.

  • g: Global replacement flag. By default, the sed command replaces the first occurrence of the pattern on each line; it does not replace other matches in the line. However, when the replacement flag is provided, all matches will be replaced.

  • /: delimiter.

  • Input_File: The file name to perform the operation.

Let's take a look at some common examples of using the sed command to search and convert text in files.

We have created the following files for demonstration.

# cat sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy after login

1) How to find and replace the "first" pattern match in each line

The sed command below replaces unix in the file with linux. This only changes the first instance of each row's pattern.

# sed 's/unix/linux/' sed-test.txt
1 Unix linux unix 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy after login

2) How to find and replace the "Nth" occurrence of the pattern in each line

Use /1, /2.../n, etc. in the line flag to replace the corresponding match.

The following sed command replaces the second instance of unix mode with linux on a line.

# sed 's/unix/linux/2' sed-test.txt
1 Unix unix linux 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy after login

3) How to search and replace all pattern instances in each line

The following sed command replaces all instances of unix format with linux, because g is a global Replace flag.

# sed 's/unix/linux/g' sed-test.txt
1 Unix linux linux 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy after login

4) How to find and replace all matching pattern instances starting from "Nth" in a line

The following sed command replaces the pattern in a line The matching instance starting from "Nth".

# sed 's/unix/linux/2g' sed-test.txt
1 Unix unix linux 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
Copy after login

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to replace strings in files in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template