sed command text processing commands of the Detailed

Detailed sed command line processing

I. Introduction

  sed command is an online editor. A character-oriented stream of non-interactive editor, which means that sed does not allow users interact with it. sed is line to process text content, which processes a row content. Handling, storing the row currently being processed in a temporary buffer, called a "model space" (pattern space), followed by treatment with the contents of the buffer sed command, the processing is completed, the contents of the buffer sent to the screen. Then the next line, which is repeated until the end of the file. File contents not changed, unless you use redirection to store the output. Sed is mainly used to automatically edit one or more documents; simplify repeated operation of the document; write conversion procedures. In the shell, use sed to bulk edit text content is very convenient.

Reference website: https://blog.51cto.com/13517084/2069074

 

Two, sed usage

sed command operation can be performed CRUD

format:

 

Sed [the OPTION] ... {Script-only- IF -NO2-OTHER-Script} [input- File] ... 
i.e. 
Sed [Option] [Action] [Output File]

 

Options:

 

 

-n: Use quiet (silent) mode. Sed in general usage, all data from STDIN generally will be listed to the terminal. However, if coupled - n-parameters, only specially treated sed through the line (or action) will be listed.
- E: directly on the command-line mode of operation of editing sed;
 -f: sed directly write operation within a document, - F filename sed operation can run in the filename;
 - R & lt: sed operation is supported regular expression syntax of the extension type. (The default is the basis of regular expression French and France)
 -i: directly modify the contents of the file to read instead of output to the terminal.
Action Description: [N1 [, N2]] function 
N1, N2: not necessarily exist, in general represents [the number of lines selected for operation], for example, if I action is required in the line between 10 and 20 , then [10, 20 [action behavior]]

 

function:

a: Add, behind a string may be connected, and these strings appear (to add the next line) in a new line 
c: substituted, may be connected to the back c string, these strings can be substituted n1, n2 between line! 
d: Delete, behind d usually do not take anything; 
i: insert, i can take back the string, and those strings come (insert on one line) in a new line; 
the p-: print, is about a choice the data printed out. Typically parameter p Sed - used with n 
s: substituted, may be substituted directly work! Usually this s action can be used with regular expression! Eg . 1 , 20S / old / new / G, the old line containing 1-20 replaced by new full

Regular metacharacters:

End of the line represents $ 
 ^ represents a first row 
[A -z0- . 9 ] represents a range of characters 
[ ^ ] Apart from showing character set character 

regular sed in \ (\) and \ {m, n \} be escaped 

. represents any character  
 * means zero or more   
\ + one or more times  
\ ? zero or one     
\ | representation or grammar

 

Three, sed example

3.1. Query

 

Prepare a txt file

 

[root@VM_0_10_centos shellScript]# cat txt.txt 
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo

 

1)显示txt文件第3行的内容

# 加-n和不加的区别(不加-n会在原文件下面打印需要打印的行。加-n只显示需要打印的行)
[root@VM_0_10_centos shellScript]# sed '3p' txt.txt 
2 this is a test
3 Are you like awk
This's a test
This's a test
10 There are orange,apple,mongo
[root@VM_0_10_centos shellScript]# sed -n '3p' txt.txt 
This's a test

2)连续显示多行信息输出到屏幕

[root@VM_0_10_centos shellScript]# sed -n '1,3p' txt.txt 
2 this is a test
3 Are you like awk
This's a test

3)显示包含"s"的行。'//':表示过滤内容,可以匹配正则表达式

[root@VM_0_10_centos shellScript]# sed -n '/s/p' txt.txt 
2 this is a test
This's a test

PS:需匹配多个条件,使用 " , " 分隔;同一行多个命令使用";"分隔

[root@VM_0_10_centos shellScript]# sed -n '1p;2p' txt.txt 
2 this is a test
3 Are you like awk

3.2.增加

使用/etc/passed文件操作,先备份好。使用备份的操作

以行为单位新增或添加

1)将 /etc/passwd 的内容列出并且列出行号,同时,将第 2~5 行删除!

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2,5d'
     1    root:x:0:0:root:/root:/bin/bash
     6    sync:x:5:0:sync:/sbin:/bin/sync
     7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8    halt:x:7:0:halt:/sbin:/sbin/halt
     9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10    operator:x:11:0:operator:/root:/sbin/nologin
# 如果只删除第二行,只需要'2d'即可

2)删除第3行到最后一行

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '3,$d'
     1    root:x:0:0:root:/root:/bin/bash
     2    bin:x:1:1:bin:/bin:/sbin/nologin

3)在第2行后面添加一行,在第2行前面添加一行

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a hello sed'
1    root:x:0:0:root:/root:/bin/bash
2    bin:x:1:1:bin:/bin:/sbin/nologin
hello sed
3    daemon:x:2:2:daemon:/sbin:/sbin/nologin

 [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2i before sed'
 1 root:x:0:0:root:/root:/bin/bash
 before sed
 2 bin:x:1:1:bin:/bin:/sbin/nologin

如果是要添加两行或以上。需在添加的信息后面接 " \ " ,然后回车,输入要添加的信息

[root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a add1\
> add2'
     1    root:x:0:0:root:/root:/bin/bash
     2    bin:x:1:1:bin:/bin:/sbin/nologin
add1
add2
     3    daemon:x:2:2:daemon:/sbin:/sbin/nologin

3.3.更新

以行为单位的替换

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/HeiDi-BoKe/p/11685621.html