Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2019-09-20 20:15:55 +08:00
commit 4f9285ae06
5 changed files with 424 additions and 421 deletions

View File

@ -0,0 +1,64 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11366-1.html)
[#]: subject: (Oracle Unleashes Worlds Fastest Database Machine Exadata X8M)
[#]: via: (https://opensourceforu.com/2019/09/oracle-unleashes-worlds-fastest-database-machine-exadata-x8m/)
[#]: author: (Longjam Dineshwori https://opensourceforu.com/author/dineshwori-longjam/)
Oracle 发布全球最快的数据库机器 Exadata X8M
======
> Exadata X8M 是第一台具有集成持久内存和 RoCE 的数据库机器。Oracle 还宣布推出 Oracle 零数据丢失恢复设备 X8MZDLRA
![](https://img.linux.net.cn/data/attachment/album/201909/20/191530qiyvvxl8qqcov8xq.jpg)
Oracle 发布了新的 Exadata 数据库机器 X8M旨在为数据库基础架构市场树立新的标杆。
Exadata X8M 结合了英特尔 Optane DC 持久存储器和通过融合以太网RoCE的 100 千兆的远程直接内存访问RDMA来消除存储瓶颈并显著提高性能其适用于最苛刻的工作负载如在线事务处理OLTP、分析、物联网、欺诈检测和高频交易。
“借助 Exadata X8M我们可以提供内存级的性能同时为 OLTP 和分析提供共享存储的所有优势”Oracle 任务关键型数据库技术执行副总裁 Juan Loaiza 说。
“使用对共享持久存储器的直接数据库访问将响应时间减少一个数量级,可加速每个 OLTP 应用程序,它是需要实时访问大量数据的应用程序的游戏规则改变者,例如欺诈检测和个性化购物,”官方补充。
### 它有什么独特之处?
Oracle Exadata X8M 使用 RDMA 让数据库直接访问智能存储服务器中的持久内存从而绕过整个操作系统、IO 和网络软件堆栈。这导致更低的延迟和更高的吞吐量。使用 RDMA 绕过软件堆栈还可以释放存储服务器上的 CPU 资源,以执行更多智能扫描查询来支持分析工作负载。
### 更少的存储瓶颈
“高性能 OLTP 应用需要高的每秒输入/输出操作IOPS和低延迟。直接数据库访问共享持久性内存可将SQL 读取的峰值性能提升至 1600 万 IOPS比行业领先的 Exadata X8 高出 2.5 倍“Oracle 在一份声明中表示。
此外Exadata X8M 通过使远程 IO 延迟低于 19 微秒,大大减少了关键数据库 IO 的延迟 —— 这比 Exadata X8 快 10 倍以上。即使对于每秒需要数百万 IO 的工作负载,也可实现这些超低延迟。
### 比 AWS 和 Azure 更高效
该公司声称,与 Oracle 最快的 Amazon RDS 存储相比Exadata X8M 的延迟降低了 50 倍IOPS 提高了 200 倍,容量提高了 15 倍。
与 Azure SQL 数据库服务存储相比Exadata X8M 的延迟降低了 100 倍IOPS 提高了 150 倍,容量提高了 300 倍。
据 Oracle 称,单机架 Exadata X8M 可提供高达 2 倍的 OLTP 读取 IOPS3 倍的吞吐量和比具有持久性内存的共享存储系统(如 Dell EMC PowerMax 8000 的单机架)低 5 倍的延迟。
“通过同时支持更快的 OLTP 查询和更高的分析工作负载吞吐量Exadata X8M 是融合混合工作负载环境以降低 IT 成本和复杂性的理想平台,”该公司说。
### Oracle 零数据丢失恢复设备 X8
Oracle 当天还宣布推出 Oracle 零数据丢失恢复设备 X8MZDLRA它使用新的 100Gb RoCE用于计算和存储服务器之间的高吞吐量内部数据传输。
Exadata 和 ZDLRA 客户现在可以在 RoCE 或基于 InfiniBand 的工程系统之间进行选择,以在其架构部署中实现最佳灵活性。
--------------------------------------------------------------------------------
via: https://opensourceforu.com/2019/09/oracle-unleashes-worlds-fastest-database-machine-exadata-x8m/
作者:[Longjam Dineshwori][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensourceforu.com/author/dineshwori-longjam/
[b]: https://github.com/lujun9972
[1]: https://i2.wp.com/opensourceforu.com/wp-content/uploads/2019/02/Oracle-Cloud.jpg?resize=350%2C212&ssl=1

View File

@ -1,67 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Oracle Unleashes Worlds Fastest Database Machine Exadata X8M)
[#]: via: (https://opensourceforu.com/2019/09/oracle-unleashes-worlds-fastest-database-machine-exadata-x8m/)
[#]: author: (Longjam Dineshwori https://opensourceforu.com/author/dineshwori-longjam/)
Oracle Unleashes Worlds Fastest Database Machine Exadata X8M
======
* _**Exadata X8M is the first database machine with integrated persistent memory and RoCE**_
* _**Oracle also announced availability of Oracle Zero Data Loss Recovery Appliance X8M (ZDLRA)**_
![][1]
Oracle has released its new Exadata Database Machine X8M with an aim to set a new bar in the database infrastructure market.
Exadata X8M combines Intel Optane DC persistent memory and 100 gigabit remote direct memory access (RDMA) over Converged Ethernet (RoCE) to remove storage bottlenecks and dramatically increase performance for the most demanding workloads such as Online Transaction Processing (OLTP), analytics, IoT, fraud detection and high frequency trading.
“With Exadata X8M, we deliver in-memory performance with all the benefits of shared storage for both OLTP and analytics,” said Juan Loaiza, executive vice president, mission-critical database technologies, Oracle.
“Reducing response times by an order of magnitude using direct database access to shared persistent memory accelerates every OLTP application and is a game changer for applications that need real-time access to large amounts of data such as fraud detection and personalized shopping,” the official added.
**Whats unique about it?**
Oracle Exadata X8M uses RDMA directly from the database to access persistent memory in smart storage servers, bypassing the entire OS, IO and network software stacks. This results in lower latency and higher throughput. Using RDMA to bypass software stacks also frees CPU resources on storage servers to execute more Smart Scan queries in support of analytics workloads.
**No More Storage Bottlenecks**
“High-performance OLTP applications require a demanding mixture of high Input/Output Operations Per Second (IOPS) with low latency. Direct database access to shared persistent memory increases peak performance to 16 million SQL read IOPS, 2.5X greater than the industry leading Exadata X8,” Oracle said in a statement.
Additionally, Exadata X8M dramatically reduces the latency of critical database IOs by enabling remote IO latencies below 19 microseconds more than 10X faster than the Exadata X8. These ultra-low latencies are achieved even for workloads requiring millions of IOs per second.
**More Efficient Better than AWS and Azure**
The company claimed that compared to the fastest Amazon RDS storage for Oracle, Exadata X8M delivers up to 50X lower latency, 200X more IOPS and 15X more capacity.
Compared to Azure SQL Database Service storage, it says, Exadata X8M delivers 100X lower latency, 150X more IOPS and 300X more capacity.
According to oracle, a single rack Exadata X8M delivers up to 2X the OLTP read IOPS, 3X the throughput and 5X lower latency than shared storage systems with persistent memory such as a single rack of Dell EMC PowerMax 8000.
“By simultaneously supporting faster OLTP queries and greater throughput for analytics workloads, Exadata X8M is the ideal platform on which to converge mixed-workload environments to decrease IT costs and complexity,” it said.
**Oracle Zero Data Loss Recovery Appliance X8**
Oracle today also announced availability of Oracle Zero Data Loss Recovery Appliance X8M (ZDLRA), which uses new 100Gb RoCE for high throughput internal data transfers between compute and storage servers.
Exadata and ZDLRA customers can now choose between RoCE or InfiniBand-based Engineered Systems for optimal flexibility in their architectural deployments.
--------------------------------------------------------------------------------
via: https://opensourceforu.com/2019/09/oracle-unleashes-worlds-fastest-database-machine-exadata-x8m/
作者:[Longjam Dineshwori][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensourceforu.com/author/dineshwori-longjam/
[b]: https://github.com/lujun9972
[1]: https://i2.wp.com/opensourceforu.com/wp-content/uploads/2019/02/Oracle-Cloud.jpg?resize=350%2C212&ssl=1

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (runningwater)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
@ -126,7 +126,7 @@ via: https://opensourceforu.com/2019/09/the-protocols-that-help-things-to-commun
作者:[Sapna Panchal][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
译者:[runningwater](https://github.com/runningwater)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -1,352 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Find and Replace a String in File Using the sed Command in Linux)
[#]: via: (https://www.2daygeek.com/linux-sed-to-find-and-replace-string-in-files/)
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
How to Find and Replace a String in File Using the sed Command in Linux
======
When you are working on text files you may need to find and replace a string in the file.
Sed command is mostly used to replace the text in a file.
This can be done using the sed command and awk command in Linux.
In this tutorial, we will show you how to do this using the sed command and then show about the awk command.
### What is sed Command
Sed command stands for Stream Editor, It is used to perform basic text manipulation in Linux. It could perform various functions such as search, find, modify, insert or delete files.
Also, its performing complex regular expression pattern matching.
It can be used for the following purpose.
* To find and replace matches with a given format.
* To find and replace specific lines that match a given format.
* To find and replace the entire line that matches the given format.
* To search and replace two different patterns simultaneously.
The fifteen examples listed in this article will help you to master in the sed command.
If you want to remove a line from a file using the Sed command, go to the following article.
**`Note:`** Since this is a demonstration article, we use the sed command without the `-i` option, which removes lines and prints the contents of the file in the Linux terminal.
But if you want to remove lines from the source file in the real environment, use the `-i` option with the sed command.
Common Syntax for sed to replace a string.
```
sed -i 's/Search_String/Replacement_String/g' Input_File
```
First we need to understand sed syntax to do this. See details about it.
* `sed:` Its a Linux command.
* `-i:` Its one of the option for sed and what it does? By default sed print the results to the standard output. When you add this option with sed then it will edit files in place. A backup of the original file will be created when you add a suffix (For ex, -i.bak
* `s:` The s is the substitute command.
* `Search_String:` To search a given string or regular expression.
* `Replacement_String:` The replacement string.
* `g:` Global replacement flag. By default, the sed command replaces the first occurrence of the pattern in each line and it wont replace the other occurrence in the line. But, all occurrences will be replaced when the replacement flag is provided
* `/` Delimiter character.
* `Input_File:` The filename that you want to perform the action.
Let us look at some examples of commonly used with sed command to search and convert text in files.
We have created the below file for demonstration purposes.
```
# 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
```
### 1) How to Find and Replace the “first” Event of the Pattern on a Line
The below sed command replaces the word **unix** with **linux** in the file. This only changes the first instance of the pattern on each line.
```
# 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
```
### 2) How to Find and Replace the “Nth” Occurrence of the Pattern on a Line
Use the /1,/2,../n flags to replace the corresponding occurrence of a pattern in a line.
The below sed command replaces the second instance of the “unix” pattern with “linux” in 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
```
### 3) How to Search and Replace all Instances of the Pattern in a Line
The below sed command replaces all instances of the “unix” format with “Linux” on the line because “g” means a global replacement.
```
# 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
```
### 4) How to Find and Replace the Pattern for all Instances in a Line from the “Nth” Event
The below sed command replaces all the patterns from the “Nth” instance of a pattern in a line.
```
# 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
```
### 5) Search and Replace the pattern on a specific line number
You can able to replace the string on a specific line number. The below sed command replaces the pattern “unix” with “linux” only on the 3rd line.
```
# sed '3 s/unix/linux/' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 6) How to Find and Replace Pattern in a Range of Lines
You can specify the range of line numbers to replace the string.
The below sed command replaces the “Unix” pattern with “Linux” with lines 1 through 3.
```
# sed '1,3 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
```
### 7) How to Find and Change the pattern in the Last Line
The below sed command allows you to replace the matching string only in the last line.
The below sed command replaces the “Linux” pattern with “Unix” only on the last line.
```
# sed '$ s/Linux/Unix/' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Unix is free and opensource operating system
```
### 8) How to Find and Replace the Pattern with only Right Word in a Line
As you might have noticed, the substring “linuxunix” is replaced with “linuxlinux” in the 6th example. If you want to replace only the right matching word, use the word-boundary expression “\b” on both ends of the search string.
```
# sed '1,3 s/\bunix\b/linux/' sed-test.txt
1 Unix linux unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 9) How to Search and Replaces the pattern with case insensitive
Everyone knows that Linux is case sensitive. To make the pattern match with case insensitive, use the I flag.
```
# sed 's/unix/linux/gI' sed-test.txt
1 linux linux linux 23
2 linux Linux 34
3 linuxlinux linuxLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 10) How to Find and Replace a String that Contains the Delimiter Character
When you search and replace for a string with the delimiter character, we need to use the backslash “\” to escape the slash.
In this example, we are going to replaces the “/bin/bash” with “/usr/bin/fish”.
```
# sed 's/\/bin\/bash/\/usr\/bin\/fish/g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /usr/bin/fish CentOS Linux OS
Linux is free and opensource operating system
```
The above sed command works as expected, but it looks bad. To simplify this, most of the people will use the vertical bar “|”. So, I advise you to go with it.
```
# sed 's|/bin/bash|/usr/bin/fish/|g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /usr/bin/fish/ CentOS Linux OS
Linux is free and opensource operating system
```
### 11) How to Find and Replaces Digits with a Given Pattern
Similarly, digits can be replaced with pattern. The below sed command replaces all digits with “[0-9]” “number” pattern.
```
# sed 's/[0-9]/number/g' sed-test.txt
number Unix unix unix numbernumber
number linux Linux numbernumber
number linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 12) How to Find and Replace only two Digit Numbers with Pattern
If you want to replace the two digit numbers with the pattern, use the sed command below.
```
# sed 's/\b[0-9]\{2\}\b/number/g' sed-test.txt
1 Unix unix unix number
2 linux Linux number
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 13) How to Print only Replaced Lines with the sed Command
If you want to display only the changed lines, use the below sed command.
* p It prints the replaced line twice on the terminal.
* n It suppresses the duplicate rows generated by the “p” flag.
```
# sed -n 's/Unix/Linux/p' sed-test.txt
1 Linux unix unix 23
3 linuxunix LinuxLinux
```
### 14) How to Run Multiple sed Commands at Once
The following sed command detect and replaces two different patterns simultaneously.
The below sed command searches for “linuxunix” and “CentOS” pattern, replacing them with “LINUXUNIX” and “RHEL8” at a time.
```
# sed -e 's/linuxunix/LINUXUNIX/g' -e 's/CentOS/RHEL8/g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 LINUXUNIX UnixLinux
linux /bin/bash RHEL8 Linux OS
Linux is free and opensource operating system
```
The following sed command search for two different patterns and replaces them with one string at a time.
The below sed command searches for “linuxunix” and “CentOS” pattern, replacing them with “Fedora30” at a time.
```
# sed -e 's/\(linuxunix\|CentOS\)/Fedora30/g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 Fedora30 UnixLinux
linux /bin/bash Fedora30 Linux OS
Linux is free and opensource operating system
```
### 15) How to Find and Replace the Entire Line if the Given Pattern Matches
If the pattern matches, you can use the sed command to replace the entire line with the new line. This can be done using the “C” flag.
```
# sed '/OS/ c New Line' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
New Line
Linux is free and opensource operating system
```
### 16) How to Search and Replace lines that Matches a Pattern
You can specify a pattern for the sed command to fit on a line. In the event of pattern matching, the sed command searches for the string to be replaced.
The below sed command first looks for lines that have the “OS” pattern, then replaces the word “Linux” with “ArchLinux”.
```
# sed '/OS/ s/Linux/ArchLinux/' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS ArchLinux OS
Linux is free and opensource operating system
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/linux-sed-to-find-and-replace-string-in-files/
作者:[Magesh Maruthamuthu][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/magesh/
[b]: https://github.com/lujun9972

View File

@ -0,0 +1,358 @@
[#]: collector: (lujun9972)
[#]: translator: (asche910)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Find and Replace a String in File Using the sed Command in Linux)
[#]: via: (https://www.2daygeek.com/linux-sed-to-find-and-replace-string-in-files/)
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
在Linux中使用sed命令如何查找和替换文件中的字符串
======
当你在使用文本文件时,很可能需要查找和替换文件中的字符串。
Sed命令主要用于替换一个文件中的文本。
在Linux中这可以通过使用sed命令和awk命令来完成。
在本教程中我们将告诉你使用sed命令如何做到这一点然后讨论讨论awk命令相关的。
### sed命令是什么
sed命令表示Stream Editor流编辑器用来在Linux上执行基本的文本操作。 它可以执行各种功能,如搜索,查找,修改,插入或删除文件。
此外,它也可以执行复杂的正则表达式匹配。
它可用于以下目的:
* 查找和替换匹配给定的格式的内容。
* 在指定行查找和替换匹配给定的格式的内容。
* 在所有行查找和替换匹配给定的格式的内容。
* 搜索并同时替换两种不同的模式。
本文列出的十五个例子可以帮助你掌握sed命令。
如果要使用sed命令删除文件中的行去下面的文章。
** `注意:`**由于这是一篇演示文章,我们使用不带`-i '选项的sed命令该命令在Linux终端中删除行并打印文件内容。
但是,在实际环境中如果你想删除源文件中的行,使用带`-i`选项的sed命令。
常见的sed替换字符串的语法。
```
sed -i 's/Search_String/Replacement_String/g' Input_File
```
首先我们需要了解sed语法来做到这一点。 请参阅有关的细节。
* `sed:` 这是一个Linux命令。
* `-i:` 这是sed命令的一个选项它有啥作用 默认情况下sed打印结果到标准输出。 当您添加使用sed这个选项时那么它会在适当的位置修改文件。 当您添加一个后缀(比如, -i.bak原始文件的备份将就会被创建。
* `s:` 字母s是一个替换命令。
* `Search_String:` 搜索一个给定的字符串或正则表达式。
* `Replacement_String:` 替换的字符串。
* `g:` 全局替换标志。 默认情况下sed命令替换每一行第一次出现的pattern模式它不会替换行中的其他匹配结果。 但是,提供了该置换标志时,所有匹配都将被替换。
* `/` 分界符。
* `Input_File:` 要执行操作的文件名。
Let us look at some examples of commonly used with sed command to search and convert text in files.
We have created the below file for demonstration purposes.
让我们来看看文件中用sed命令来搜索和转换文本的一些常用例子。
我们已经创建了用于演示的以下文件。
```
# 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
```
### 1) 在一行中如何查找和替换Pattern模式的“第一次”匹配
下面的sed命令用**linux**替换文件中的**unix**。 这仅仅改变了每一行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
```
### 2) 如何查找和替换每一行中“第N次”出现的模式
在行中使用/1,/2,../n标志来代替相应的匹配。
下面的sed命令在一行中用“linux”来替换“unix”模式的第二个实例。
```
# 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
```
### 3) 在一行中如何搜索和替换模式的所有实例
The below sed command replaces all instances of the “unix” format with “Linux” on the line because “g” means a global replacement.
下面的sed命令用“Linux”替换“unix”格式的所有实例因为“G”是指一个全局的替换标志。
```
# 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
```
### 4) 在一行的“第N个”匹配中如何查找和替换模式的所有实例
下面的sed命令在一行中且从模式的“第n个”实例中替换所有的模式。
```
# 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
```
### 5) 在特定的行号搜索和替换模式
你可以替换特定行号中的字符串。 下面的sed命令用“linux”仅替换第三行的“unix”模式。
```
# sed '3 s/unix/linux/' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxlinux UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 6) 在特定范围行号间搜索和替换模式
You can specify the range of line numbers to replace the string.
The below sed command replaces the “Unix” pattern with “Linux” with lines 1 through 3.
您可以指定行号的范围,以替换字符串。
下面的sed命令在1到3行间用“Linux”替换“Unix”模式。
```
# sed '1,3 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
```
### 7) 如何查找和修改最后一行的模式
下面的sed命令允许您只在最后一行更换匹配的字符串。
下面的sed命令只在最后一行用“Unix”替换“Linux”模式。
```
# sed '$ s/Linux/Unix/' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Unix is free and opensource operating system
```
### 8) 在一行中如何只查找和替换正确的模式匹配
你可能已经注意到子串“linuxunix”被替换为在第6例中的“linuxlinux”。 如果您只想更改正确的匹配词,用这个边界符“\b”上的搜索串的两端。
```
# sed '1,3 s/\bunix\b/linux/' sed-test.txt
1 Unix linux unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 9) 如何以不区分大小写来搜索与替换模式
大家都知道Linux是区分大小写的。 为了与不区分大小写的模式匹配使用I标志。
```
# sed 's/unix/linux/gI' sed-test.txt
1 linux linux linux 23
2 linux Linux 34
3 linuxlinux linuxLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 10) 如何查找和替换包含分隔符的字符串
当您搜索和替换含分隔符的字符串时,我们需要用反斜杠“\”来取消转义。
在这个例子中,我们将用“/usr/bin/fish”来替换“/bin/bash”。
```
# sed 's/\/bin\/bash/\/usr\/bin\/fish/g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /usr/bin/fish CentOS Linux OS
Linux is free and opensource operating system
```
上述sed命令按预期工作但它看起来来很糟糕。 为了简化,大部分的人会用竖线“|”。 所以,我建议你去用它。
```
# sed 's|/bin/bash|/usr/bin/fish/|g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /usr/bin/fish/ CentOS Linux OS
Linux is free and opensource operating system
```
### 11) 如何以给定的模式来查找和替换数字
类似地,数字可以用模式来代替。 下面的sed命令替换所有数字以“[0-9]”“number”的模式。
```
# sed 's/[0-9]/number/g' sed-test.txt
number Unix unix unix numbernumber
number linux Linux numbernumber
number linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 12) 如何用模式仅查找和替换两个数字
如果你想用模式来代替两位数字使用下面的sed命令。
```
# sed 's/\b[0-9]\{2\}\b/number/g' sed-test.txt
1 Unix unix unix number
2 linux Linux number
3 linuxunix UnixLinux
linux /bin/bash CentOS Linux OS
Linux is free and opensource operating system
```
### 13) 如何用sed命令仅打印被替换的行
如果你想显示仅更改的行使用下面的sed命令。
* p - 它输出替换的行在终端上两次。
* n - 它抑制由“p”标志所产生的重复行。
```
# sed -n 's/Unix/Linux/p' sed-test.txt
1 Linux unix unix 23
3 linuxunix LinuxLinux
```
### 14) 如何同时运行多个sed命令
以下sed命令检测和同时置换两个不同的模式。
下面的sed命令搜索为“linuxunix”和“CentOS的”模式用“LINUXUNIX”和“RHEL8”一次性更换他们。
```
# sed -e 's/linuxunix/LINUXUNIX/g' -e 's/CentOS/RHEL8/g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 LINUXUNIX UnixLinux
linux /bin/bash RHEL8 Linux OS
Linux is free and opensource operating system
```
下面的sed命令搜索替换两个不同的模式每次以一个字符串。
以下sed的命令搜索 为“linuxunix”和“CentOS的”模式用“Fedora30”替换它们。
```
# sed -e 's/\(linuxunix\|CentOS\)/Fedora30/g' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 Fedora30 UnixLinux
linux /bin/bash Fedora30 Linux OS
Linux is free and opensource operating system
```
### 15) 如何查找和替换整个行,如果给定的模式匹配
如果模式匹配可以使用sed命令用新行来代替整行。 这可以通过使用“C”标志来完成。
```
# sed '/OS/ c New Line' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
New Line
Linux is free and opensource operating system
```
### 16) 如何搜索和替换相匹配的模式行
在一行中您可以为sed命令指定一种模式以适应。 在模式匹配的情况下sed命令搜索要被替换的字符串。
下面的sed命令首先查找具有“OS”模式的行然后用“ArchLinux”替换单词“Linux”。
```
# sed '/OS/ s/Linux/ArchLinux/' sed-test.txt
1 Unix unix unix 23
2 linux Linux 34
3 linuxunix UnixLinux
linux /bin/bash CentOS ArchLinux OS
Linux is free and opensource operating system
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/linux-sed-to-find-and-replace-string-in-files/
作者:[Magesh Maruthamuthu][a]
选题:[lujun9972][b]
译者:[Asche910](https://github.com/asche910)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/magesh/
[b]: https://github.com/lujun9972