Why should I log the sed command in detail:
sed is good at fetching lines. The Three Musketeers use the most frequently in work. This article will add, delete, modify and check the commonly used sed commands.
Explain in detail in case the query is forgotten in future work. The sed command is a must-have skill for operation and maintenance personnel.
If you don't know the Three Musketeers, then you don't need to do operation and maintenance.
Introduction to sed:
It is a stream editor, which is a very useful tool in text processing. It can be used perfectly with regular expressions and has extraordinary functions. deal with
, store the currently processed line in a temporary buffer called "pattern space", and then use the sed command to process the buffer.
After processing the contents of the buffer, send the contents of the buffer to the screen. Then the next line is processed, and so on, until the end of the file.
The file contents are not changed unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify the editing of files
repeated operations; writing conversion programs, etc.
Add test text:
cat >sed.log<<"EOF"
101,$oldboy,CEO
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
105,$feixue,CIO
>>"EOF"
|
sed syntax format:
sed [options] [commands] [input files]
The execution flow of the sed command:
The sed command is to read the file line by line into the memory and process it as a line. It is also called a stream editor. Like the pipeline in the workshop, it is processed line by line. After getting the required content, display it on the screen.
sed principle:
After sed reads a line of content, sed determines whether the condition is met
sed version view: sed -v
sed basic parameters:
-n suppress default output
-r supports regular expressions
-p print
-e multiple edits
-i.bak
backup after modification
s search once
sg search global
# # # : s#before replacement#after replacement#g
/ / / : it's the same as
|
sed function details:
sed adds:
-i : add content to the next line of XX line
-a: add content to the line above line XX
sed i parameter: demo
increase:
Case 1: sed single line increase
[root@chenleilei ~]# sed '2i nihaoya' sed.log 101,$oldboy,CEO nihaoya 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
Case 2: Add multiple lines before the sed line
[root@chenleilei ~]# sed '2i nihaoya\nwohenhao' sed.log 101,$oldboy,CEO nihaoya wohenhao 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
-a: demo
【Add】Case 1: Single line increase after sed line
[root@chenleilei ~]# sed '2a nihaoya' sed.log 101,$oldboy,CEO 102,$zhangyao,CTO nihaoya 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
【Add】 Case 2: Add multiple lines after the sed line
[root @ chenleilei ~] # sed '2a nihaoya \ nwohenhao' sed.log
101,$oldboy,CEO
102,$zhangyao,CTO
nihaoya
wohenhao
103,$Alex,COO
104,$yy,CFO
105,$feixue,CIO
|
【Add】 Case 3: sed add sed -e to different lines of sed
[root @ chenleilei ~] # sed -e '2i 123' -e '5i 456' sed.log 101, $ oldboy, CEO 123 102, $ zhangyao, CTO 103, $ Alex, COO 104, $ yy, CFO 456 105 , $ feixue, CIO |
sed delete(d)
[Delete] sed delete case 1: delete the specified line
[root@chenleilei ~]# sed '4d' sed.log 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO 105,$feixue,CIO |
[Delete] sed delete case 2: delete consecutive lines
[root@chenleilei ~]# sed '2,4d' sed.log 101,$oldboy,CEO 105,$feixue,CIO |
[Delete] sed delete case 3: delete discontinuous lines
seq 10 |sed -e{2,4,8}d ##Note that quotation marks or double quotation marks cannot be added here, otherwise an error will be reported |
[Delete] sed delete case 4: delete lines matching n and n
[root@chenleilei ~]# sed -nr '/103|105/!p' sed.log
101,$oldboy,CEO 102,$zhangyao,CTO 104,$yy,CFO ### ! Delete all lines except 103.105 and print |
【Change . Replace] sed modification case 1:
Revise:
Modify a single character
[root@chenleilei ~]# sed 's#102#1031#g' sed.log 101,$oldboy,CEO 1031,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
small issue:
When echo is used to append multiple lines to a file, you need to use echo -e
【Query】 | is a regular symbol, you need to add r
[display] 1 to 4 lines
[root@chenleilei ~]# sed -n '1,4p' sed.log
101,$oldboy,CEO
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
|
【Query】 | is a regular symbol, you need to add r
Exclude 1-4 lines
[root@chenleilei ~]# sed -n '1,4!p' sed.log 105,$feixue,CIO |
[查询]显示2 4 两行
[root@chenleilei ~]# sed -nr '/2|4/p' sed.log 102,$zhangyao,CTO 104,$yy,CFO |
[查询] 查询103开头的行
[root@chenleilei ~]# sed -n '/^103/p' sed.log 103,$Alex,COO |
[查询] 查询103结尾的行
[root@chenleilei ~]# sed -n '/$103/p' sed.log 103,$Alex,COO,103 |
[查询] 查询103开头
到105开头的行(正则)
[root@chenleilei ~]# sed -n '/^103/,/^105/p' sed.log 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
[查询] 查询 105以及 和 103 开头的行(正则 需要加 r)
[root@chenleilei ~]# sed -nr '/^103|^105/p' sed.log 103,$Alex,COO 105,$feixue,CIO |
[排除] 排除一行
[root@chenleilei ~]# sed -n '1!p' sed.log 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
[排除] 排除多行
[root@chenleilei ~]# sed -n '1,2!p' sed.log 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO |
[排除不连续的行]
[root@chenleilei ~]# sed -nr '/102|104/!p' sed.log 101,$oldboy,CEO 103,$Alex,COO 105,$feixue,CIO |