linux sed command usage example

table of Contents

1. Introduction

Two, sed use parameters

2.1. Add/delete by line unit

2.2. Replacement and display by line unit

2.3. Data search and display

2.4. Data search and delete

2.5. Search for data and execute commands

2.6. Search and replace data


1. Introduction

Sed is an online editor that processes content one line at a time. During processing, the currently processed line is stored in a temporary buffer called "pattern space", and then the content in the buffer is processed with the sed command. After the processing is completed, the content of the buffer is sent to the screen. Then process the next line, and repeat this way until the end of the file. The content of the file does not change unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.

Two, sed use parameters

[root@client ~]# sed
选项与参数:
-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。

动作说明: [n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』

function:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

2.1. Add/delete by line unit

 1. List the content of deviceInfo.txt and print the line number. At the same time, please delete lines 2~5!

[root@client kangll]# nl deviceInfo.txt | sed '2,5d'

The action of sed is '2,5d', and that d is delete! Because line 2-5 was deleted for him, the displayed data is not line 2-5~ In addition, please note that sed -e should have been issued originally, it is fine without -e! At the same time, please note that the action following sed must be enclosed in two single quotation marks!

2. Delete the second line or the content between the second line and the last line

3. After the second line (that is, add it to the third line), add the word "kangll"

4. Add "kangll" before the second line

[root@client kangll]# nl deviceInfo.txt | sed '2i kangll'

2.2. Replacement and display by line unit

1. Replace the content of lines 2-5 with [No 2-5 number]

[root@client kangll]# nl deviceInfo.txt | sed '2,5c No 2-5 Number'

2. List only lines 3-6 in the deviceInfo.txt file

### 只显示 3,6 行的内容
[root@client kangll]# nl deviceInfo.txt | sed -n '3,6p'
     3	T0905,T0000000001,120.23.34.123,3,2016-6-19 0:00,2016-6-19 16:04,
     4	T0905,T0000000001,111.23.34.21,9,2016-6-22 0:00,2016-6-22 13:02,
     5	T0905,T0000000001,111.23.34.21,5,2016-6-22 0:00,2016-6-22 16:23,
     6	t9.ipva.cn,T0000000001,120.23.34.21,1,2016-6-23 0:00,2016-6-23 10:53,

2.3. Data search and display

 nl sed_test.txt | sed '/T0908.ipva.cn/p'

When using sed -n only print lines that contain matches 

[root@datanode01 kangll]# nl sed_test.txt | sed -n '/T0908.ipva.cn/p'  # 只会输出 匹配行
     1  T0908.ipva.cn,T1111111111,111.111.111.111,0,2020-06-29T10:10:30+08:00,2020-06-29 10:15,

2.4. Data search and delete

[root@datanode01 kangll]# nl sed_test.txt | sed '/T0908.ipva.cn/d'

2.5. Search for data and execute commands

Search for the line corresponding to [T0908.ipva.cn] in sed_test.txt, execute a set of commands in the curly brackets, separate each command with a semicolon, replace [111.111.111.111] with [1.1.1.1], Output this line

2.6. Search and replace data

In addition to the processing mode of the entire line, sed can also search for and replace part of the data with the line unit. Basically, the search and substitution of sed is quite similar to vi! He is kind of like this:

sed 's/要被取代的字串/新的字串/g'
# 查看原始的数据
[root@datanode01 kangll]# nl sed_test.txt
     1  T0908.ipva.cn,T1111111111,111.111.111.111,0,2020-06-29T10:10:30+08:00,2020-06-29 10:15,
     2  T0907.ipva.cn,T2000000102,120.110.34.21,12,2016-06-23 09:53:00,2020-06-23 09:53,
     3  T0906.ipva.cn,T3000000102,120.110.34.21,12,2020-06-22T15:34:30+08:00,2019-04-13 2:06,
     4  T0905.ipva.cn,T4000000104,120.110.34.21,12,2020-06-22T15:34:30+08:00,2019-04-18 2:44,
     5  T0904.ipva.cn,T5000000105,120.110.34.21,12,2020-06-22T15:34:30+08:00,2019-08-24 6:20,
# 搜索 [T5000000105] 这个设备 将  ‘T’ 之前的内容删掉
[root@datanode01 kangll]# nl sed_test.txt | grep T5000000105 | sed 's/^.*T//g'
15:34:30+08:00,2019-08-24 6:20,
#  搜索 [T5000000105] 这个设备 将  ‘T’ 之前的内容删掉  ,再将  ‘,’之后的内容删掉
[root@datanode01 kangll]# nl sed_test.txt | grep T5000000105 | sed 's/^.*T//g' | sed 's/,.*$//g'
15:34:30+08:00
[root@datanode01 kangll]#

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_35995514/article/details/112099905