本文是正在进行的Unix Sed教程系列的一部分。在之前的文章中,我们讨论了 sedprint operation 和 seddelete operation.
In this article let us review how to use sedsubstitute command “s”.
的`s’命令可能是`sed中最重要的’并且有很多不同的选择。
的`s’命令尝试将样式空间与提供的REGEXP匹配;如果匹配成功,则将匹配的模式空间部分替换为REPLACEMENT。
Syntax: #sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filename #sed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename
- s是替代命令
- /是定界符
- REGEXP是匹配的正则表达式
- 替换是要替换的价值
标志可以是以下任意一种
- g将所有REGEXP实例替换为REPLACEMENT
- n可以是任何数字,用REPLACEMENT代替REGEXP的第n个实例。
- p If substitution was made, then prints the new pattern 空间.
- 我以不区分大小写的方式匹配REGEXP。
- w文件如果进行替换,则将结果写到给定文件中。
- 我们可以使用不同的定界符(@%;:之一)代替/
让我们首先创建thegeekstuff.txt文件,该文件将在下面提到的所有示例中使用。
$ cat thegeekstuff.txt # Instruction Guides 1. 的Linux Sysadmin, 的Linux Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux 5. Productivity (Too many technologies to explore, not much time available) # Additional FAQS 6. Windows- Sysadmin, reboot etc.
现在让我们回顾一些有趣的替换示例。
1.代词“Linux” to “Linux-Unix” Using seds//
在下面的示例中,在输出行中“1. 的Linux-Unix Sysadmin, 的Linux Scripting etc” 上 ly first 的Linux is replaced 通过 的Linux-Unix. If no flags are specified the first match of line is replaced.
$ sed's/Linux/Linux-Unix/' thegeekstuff.txt # Instruction Guides 1. 的Linux-Unix Sysadmin, 的Linux Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux-Unix 5. Productivity (Too many technologies to explore, not much time available) # Additional FAQS 6. Windows- Sysadmin, reboot etc.
2. Substitute all Appearances of a Word Using seds//g
的below sedcommand replaces all occurrences of 的Linux to 的Linux-Unix using global substitution flag “g”.
$ sed's/Linux/Linux-Unix/g' thegeekstuff.txt # Instruction Guides 1. 的Linux-Unix Sysadmin, 的Linux-Unix Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux-Unix 5. Productivity (Too many technologies to explore, not much time available) # Additional FAQS 6. Windows- Sysadmin, reboot etc.
3. Substitute Only 2nd Occurrence of a Word Using seds//2
在下面的示例中,在输出行中“1. 的Linux Sysadmin, 的Linux-Unix Scripting etc.” 上 ly 2nd occurance of 的Linux is replaced 通过 的Linux-Unix.
$ sed's/Linux/Linux-Unix/2' thegeekstuff.txt # Instruction Guides 1. 的Linux Sysadmin, 的Linux-Unix Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux 5. Productivity (Too many technologies to explore, not much time available) # Additional FAQS 6. Windows- Sysadmin, reboot etc.
4. Write Changes to a File 和 Print the Changes Using seds//gpw
的example below has substitution with three flags. It substitutes all the occurance of 的Linux to 的Linux-Unix 和 prints the substituted output as well as written the same to the given the file.
$ sed-n 's/Linux/Linux-Unix/gpw output' thegeekstuff.txt 1. 的Linux-Unix Sysadmin, 的Linux-Unix Scripting etc. 4. Storage in 的Linux-Unix $ cat output 1. 的Linux-Unix Sysadmin, 的Linux-Unix Scripting etc. 4. Storage in 的Linux-Unix
5.仅当使用sed将线与图案匹配时才替换
在此示例中,如果行与模式匹配“-“,然后替换的所有字符“-” with the empty.
$ sed'/\-/s/\-.*//g' thegeekstuff.txt # Instruction Guides 1. 的Linux Sysadmin, 的Linux Scripting etc. 2. Databases 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux 5. Productivity (Too many technologies to explore, not much time available) # Additional FAQS 6. Windows
6.使用sed从每行中删除最后X个字符
这个sedexample deletes last 3 characters from each line.
$ sed's/...$//' thegeekstuff.txt # Instruction Gui 1. 的Linux Sysadmin, 的Linux Scripting e 2. Databases - Oracle, mySQL e 3. Security (Firewall, Network, Online Security e 4. Storage in Li 5. Productivity (Too many technologies to explore, not much time availab # Additional F 6. Windows- Sysadmin, reboot e
7.使用sed消除评论
Delete all the comment lines from a file as shown below using sedcommand.
$ sed-e 's/#.*//' thegeekstuff.txt 1. 的Linux Sysadmin, 的Linux Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux 5. Productivity (Too many technologies to explore, not much time available) 6. Windows- Sysadmin, reboot etc.
8.使用sed消除注释和空白行
在此示例中,有两个命令由‘;’
- 第一个命令将以#开头的行替换为空白行
- 第二条命令删除空行。
$ sed-e 's/#.*//;/^$/d' thegeekstuff.txt 1. 的Linux Sysadmin, 的Linux Scripting etc. 2. Databases - Oracle, mySQL etc. 3. Security (Firewall, Network, Online Security etc) 4. Storage in 的Linux 5. Productivity (Too many technologies to explore, not much time available) 6. Windows- Sysadmin, reboot etc.
9.使用sed将DOS换行符(CR / LF)转换为Unix格式
将DOS文件复制到Unix,您可以在每行末尾找到\ r \ n。
这个example converts the DOS file format to Unix file format using sedcommand.
$ sed's/.$//' filename
10.使用sed消除文件中的HTML标记
In this example, the regular expression given in the sedcommand matches the html tags 和 replaces with the empty.
$ sed-e 's/<[^>]*>//g' This <b> is </b> an <i>example</i>. This is an example.
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
一个人将如何重新排列ls -al输出的列?
在缺少gnu / 的Linux的AIX上’的出色功能,我需要ls -al才能输出 …
的filename -rwxrwxrwx 1 root root 12345678 …
… instead of …
-rwxrwxrwx 1 root root 12345678 Jan 01 2009 的filename
谢谢!