博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix Sed Tutorial3 : Find and Replace Text Inside a File Using RegEx
阅读量:2218 次
发布时间:2019-05-08

本文共 5954 字,大约阅读时间需要 19 分钟。

his article is part of on-going Unix Sed Tutorial series. In previous articles, we discussed about  and.

In this article let us review how to use sed substitute command “s”.

The `s’ command is probably the most important in `sed’ and has a lot of different options.

The `s’ command attempts to match the pattern space against the supplied REGEXP; if the match is successful, then that portion of the pattern space which was matched is replaced with REPLACEMENT.

Syntax:#sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filename#sed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename
  • s is substitute command
  • / is a delimiter
  • REGEXP is regular expression to match
  • REPLACEMENT is a value to replace

FLAGS can be any of the following

  • g Replace all the instance of REGEXP with REPLACEMENT
  • n Could be any number,replace nth instance of the REGEXP with REPLACEMENT.
  • p If substitution was made, then prints the new pattern space.
  • i match REGEXP in a case-insensitive manner.
  • w file If substitution was made, write out the result to the given file.
  • We can use different delimiters ( one of @ % ; : ) instead of /

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

$ cat thegeekstuff.txt# Instruction Guides1. Linux Sysadmin, Linux Scripting etc.2. Databases - Oracle, mySQL etc.3. Security (Firewall, Network, Online Security etc)4. Storage in Linux5. Productivity (Too many technologies to explore, not much time available)#  Additional FAQS6. Windows- Sysadmin, reboot etc.

Let us review some interesting examples for substitution now.

1. Substitute Word “Linux” to “Linux-Unix” Using sed s//

In the example below, in the output line “1. Linux-Unix Sysadmin, Linux Scripting etc” only first Linux is replaced by Linux-Unix. If no flags are specified the first match of line is replaced.

$ sed 's/Linux/Linux-Unix/' thegeekstuff.txt# Instruction Guides1. Linux-Unix Sysadmin, Linux Scripting etc.2. Databases - Oracle, mySQL etc.3. Security (Firewall, Network, Online Security etc)4. Storage in Linux-Unix5. Productivity (Too many technologies to explore, not much time available)#  Additional FAQS6. Windows- Sysadmin, reboot etc.

2. Substitute all Appearances of a Word Using sed s//g

The below sed command replaces all occurrences of Linux to Linux-Unix using global substitution flag “g”.

$ sed 's/Linux/Linux-Unix/g' thegeekstuff.txt# Instruction Guides1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.2. Databases - Oracle, mySQL etc.3. Security (Firewall, Network, Online Security etc)4. Storage in Linux-Unix5. Productivity (Too many technologies to explore, not much time available)#  Additional FAQS6. Windows- Sysadmin, reboot etc.

3. Substitute Only 2nd Occurrence of a Word Using sed s//2 (替换每一行的第二个) 

In the example below, in the output line “1. Linux Sysadmin, Linux-Unix Scripting etc.” only 2nd occurance of Linux is replaced by Linux-Unix.

$ sed 's/Linux/Linux-Unix/2' thegeekstuff.txt# Instruction Guides1. Linux Sysadmin, Linux-Unix Scripting etc.2. Databases - Oracle, mySQL etc.3. Security (Firewall, Network, Online Security etc)4. Storage in Linux5. Productivity (Too many technologies to explore, not much time available)#  Additional FAQS6. Windows- Sysadmin, reboot etc.

4. Write Changes to a File and Print the Changes Using sed s//gpw

The example below has substitution with three flags. It substitutes all the occurance of Linux to Linux-Unix and prints the substituted output as well as written the same to the given the file.

$ sed -n 's/Linux/Linux-Unix/gpw output' thegeekstuff.txt1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.4. Storage in Linux-Unix$ cat output1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.4. Storage in Linux-Unix

5. Substitute Only When the Line Matches with the Pattern Using sed

In this example, if the line matches with the pattern “-”, then it replaces all the characters from “-” with the empty.

$ sed '/\-/s/\-.*//g' thegeekstuff.txt# Instruction Guides1. Linux Sysadmin, Linux Scripting etc.2. Databases3. Security (Firewall, Network, Online Security etc)4. Storage in Linux5. Productivity (Too many technologies to explore, not much time available)#  Additional FAQS6. Windows

6. Delete Last X Number of Characters From Each Line Using sed

This sed example deletes last 3 characters from each line.

$ sed 's/...$//' thegeekstuff.txt# Instruction Gui1. Linux Sysadmin, Linux Scripting e2. Databases - Oracle, mySQL e3. Security (Firewall, Network, Online Security e4. Storage in Li5. Productivity (Too many technologies to explore, not much time availab#  Additional F6. Windows- Sysadmin, reboot e

7. Eliminate Comments Using sed

Delete all the comment lines from a file as shown below using sed command.

$  sed -e 's/#.*//' thegeekstuff.txt1. Linux Sysadmin, Linux Scripting etc.2. Databases - Oracle, mySQL etc.3. Security (Firewall, Network, Online Security etc)4. Storage in Linux5. Productivity (Too many technologies to explore, not much time available)6. Windows- Sysadmin, reboot etc.

8. Eliminate Comments and Empty Lines Using sed

In this example, there are two commands seperated by ‘;’

  • First command replaces the lines starting with the # to the blank lines
  • Second command deletes the empty lines.
$ sed -e 's/#.*//;/^$/d'  thegeekstuff.txt1. Linux Sysadmin, Linux Scripting etc.2. Databases - Oracle, mySQL etc.3. Security (Firewall, Network, Online Security etc)4. Storage in Linux5. Productivity (Too many technologies to explore, not much time available)6. Windows- Sysadmin, reboot etc.

9. Convert DOS newlines (CR/LF) to Unix format Using sed

Copy the DOS file to Unix, you could find \r\n in the end of each line.

This example converts the DOS file format to Unix file format using sed command.

$sed 's/.$//' filename

10. Eliminate HTML Tags from file Using sed

In this example, the regular expression given in the sed command matches the html tags and replaces with the empty.

$ sed -e 's/<[^>]*>//g'This  is  an example.This  is  an example.

转载地址:http://euffb.baihongyu.com/

你可能感兴趣的文章
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Loadrunner】性能测试报告实战
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>
【雅思】雅思需要购买和准备的学习资料
查看>>