Detailed explanation of zero foundation to proficient sed command

preface

This section explains the usage of sed in detail, combined with the regular expressions learned before. The article is completely from Xiaobai's point of view. It took a long time to edit it so that everyone can understand and operate it as much as possible.

sed introduction

1. sed introduction

vi is to edit text interactively, usually adding, deleting and modifying text data with the keyboard. The sed command is different. It adopts the stream editing mode. Before processing data, it needs to provide rules in advance. It will edit data according to this rule.
sed processes the data in the text file through commands, which can be input through the command line or stored in the text file. Match and modify the data through the specified rules.
Note that sed does not directly modify the source file data by default, but copies the data to the buffer for operation;
Its execution sequence is to read only one line at a time. When one line of data matches, continue to read the next line of data and repeat this process until all data in the file is processed.

2.sed syntax introduction

[ root@zaishu ~]#Sed [options] [script command] file name

optionmeaning
-e script commandThis option adds the script command that follows it to the existing command.
-f script command fileThis option adds script commands from subsequent files to existing commands.
-nBy default, sed will automatically output the processed content after all specified scripts are executed, and this option will mask the startup output. You need to use the print command to complete the output.
-iThis option will directly modify the source file. Use it with caution.
-r : -r: sed's actions support the syntax of extended normal notation. (default is basic normal notation syntax)

The focus is on how the script command is defined.

3.sed script command

Syntax:
[ root@zaishu ~]#Sed [options] [script command] file name
This chapter will introduce the most important * * [script command] in sed in detail**

1. Printing

In sed, you can also operate functions similar to grep to filter and print text content according to keywords.
The syntax format is as follows:

sed (-n) [address]p   // Full output  
sed  (-n)  [address]/p  
-n :  By default, sed After the specified execution of the script, the processed full-text content will be automatically output, and this option will mask the startup output, which needs to be used p Command to output only the processed content.
address Indicates that the line to be operated is specified. If it is not specified, the default is to act on all text. Regular expressions are also used to specify
p: Those lines that match the string, print out (output to the screen)

Example 1. Print full text

[root@zaishu ~]# sed -n p /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
...
oracle:x:1002:1002::/home/oracle:/bin/bash

Example 2. Match the specified character

Print contains zaishu Line of
[root@zaishu zaishu]# sed -n '/zaishu/p' sedtxt 
welcome to zaishu.cn welcome to zaishu.cn
welcome to zaishu.cn welcome to zaishu.cn

Example 3. Matching using regular expressions
For details on the use of regular expressions, please refer to the previous section. It is also applicable in sed.

Print hello First line
[root@zaishu zaishu]# sed -n '/^hello/p' sedtxt 
hello sed

Print the line containing 7 or 8 digits and the second line

[root@zaishu zaishu]# sed -n '/[7-8]\|2/p' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
oracle:x:1002:1002::/home/oracle:/bin/bash

2.s replacement

[ root@zaishu ~]#Sed [options] [script command] file name
//In order to facilitate the explanation of replacement, there is no [option] in the replacement, focusing on [script command].

s replacement script command format is as follows:

The format is:[address]s/pattern/replacement/flags
address Indicates that the line to be operated is specified. If it is not specified, the default is to act on all text(For specific usage, the following examples do not specify full-text operation). 
pattern Refers to the content before replacement
replacement Refers to the new content after replacement.
flags: Its function is to do secondary filtering or other actions on the matched content.
flagsfunction
n1 ~ 512 indicates the number of times the specified string to be replaced appears before replacement. For example, there are three C's in a line, but you only want to replace the second C. use this flag
gGlobal replacement means to replace all matched contents. No g, only the first match will be replaced. For example, if there are three CS in a row of data, only the first C will be replaced;
pPrint additional matching lines. Usually used with option - n (refer to sed syntax), which means that only matching rows are printed.
w fileSave the data in the buffer to the file file;
replacementfunction
&Reference matching content
\1,2...nReference the nth content of the match, and specify the matching content with \ (\) grouping. You can refer to the grouping explained in the previous regular expression

Example 1. Replace the first string

[root@zaishu zaishu]# sed  's/shu/su/' sedtxt 
hello sed
welcome to zaisu.cn welcome to zaishu.cn
welcome to zaisu.cn welcome to zaishu.cn

[root@zaishu zaishu]# cat sedtxt 
hello sed
welcome to zaishu.cn welcome to zaishu.cn
welcome to zaishu.cn welcome to zaishu.cn

The following knowledge points can be obtained from this example:

  1. When the script command does not specify n, it will only process the first string
  2. If the address is not specified, the full text is matched
  3. After the command is executed, the output content is the full text
  4. By default, the original content is not replaced.

Example 2. Replace only the second string in the line

[root@zaishu zaishu]# sed 's/shu/su/2' sedtxt 
hello sed
welcome to zaishu.cn welcome to zaisu.cn   //The first shu does not replace, only the second in each line
welcome to zaishu.cn welcome to zaisu.cn

Example 3. Match all strings on each line

[root@zaishu zaishu]# sed  's/shu/su/g' sedtxt 
hello sed
welcome to zaisu.cn welcome to zaisu.cn
welcome to zaisu.cn welcome to zaisu.cn

Example 4. Print only the replaced lines

In script commands p Lines representing additional print operations, as follows
[root@zaishu zaishu]# sed 's/shu/su/p' sedtxt 
hello sed
welcome to zaisu.cn welcome to zaishu.cn
welcome to zaisu.cn welcome to zaishu.cn
welcome to zaisu.cn welcome to zaishu.cn
welcome to zaisu.cn welcome to zaishu.cn

Print only the lines of the operation

It needs to be combined at this time sed In the command-n Options (Reference) sed Syntax), which means that only lines for printing operations are combined, and these two combinations are used very frequently.
[root@zaishu zaishu]# sed -n 's/shu/su/p' sedtxt 
welcome to zaisu.cn welcome to zaishu.cn
welcome to zaisu.cn welcome to zaishu.cn

Example 5. Save the content of the operation to the text

g Represents full-text processing w Indicates that the replaced row is saved to sed2 This text
[root@zaishu zaishu]# sed -n 's/shu/su/gw sed2' sedtxt 
[root@zaishu zaishu]# cat sed2
welcome to zaisu.cn welcome to zaisu.cn
welcome to zaisu.cn welcome to zaisu.cn

Example 6. Escape
If the matched content contains special characters, like the string of the path, the forward slash in the path needs to be escaped.
Replace / home/mysql with / hm/my

[root@zaishu ~]# sed -n 's/\/home\/mysql/\/hm\/my/p' /etc/passwd
mysql:x:1001:1001::/hm/my:/sbin/nologin

Example 7. The replaced text refers to the previous matching content
The replaced text can refer to the previous matching content.

replacementfunction
&Reference matching content
\1,2...nReference the nth content of the match, and specify the matching content with \ (\) grouping. You can refer to the grouping explained in the previous regular expression
& : Reference previous matches, zaishu Become www.zaishu
[root@zaishu zaishu]# sed -n 's/zaishu/www.&/p' sedtxt 
welcome to www.zaishu.cn welcome to zaishu.cn
welcome to www.zaishu.cn welcome to zaishu.cn

3. Delete script command d

In the sed script, the command d is used to delete the matching lines in the buffer.
The syntax format is as follows:

sed  [address]/d   //Delete the address range and the line matching the content
address:Indicates that the line to be operated is specified. If it is not specified, the default is to act on all text. You can perform conditional matching and support regular expressions
d:  delete

Example 1. Delete all rows
The effect is that nothing is output.

[root@zaishu zaishu]# cat sedtxt2 
This is zaishu.cn 1.
This is zaishu.cn 2.
This is zaishu.cn 3.
This is zaishu.cn 4.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 11.
This is zaishu.cn 12.
This is zaishu.cn 13.
This is zaishu.cn 14.

[root@zaishu zaishu]# Sed'd 'sedtxt2 / / no output

Example 2. Delete the specified line

Delete first row
[root@zaishu zaishu]# sed '1d' sedtxt2 
This is zaishu.cn 2.
This is zaishu.cn 3.
This is zaishu.cn 4.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 11.
This is zaishu.cn 12.
This is zaishu.cn 13.
This is zaishu.cn 14.

Example 3. Delete multiple lines
Delete lines 2-4

[root@zaishu zaishu]# sed '2,4d' sedtxt2 
This is zaishu.cn 1.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 11.
This is zaishu.cn 12.
This is zaishu.cn 13.
This is zaishu.cn 14.

Or use two text modes to specify the range

[root@zaishu zaishu]# sed '/2/,/4/d' sedtxt2 
This is zaishu.cn 1.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 11.

Example 4. $can represent the end
The following shows all lines after deleting 3 lines

[root@zaishu zaishu]# sed '3,$d' sedtxt2 
This is zaishu.cn 1.
This is zaishu.cn 2.

Example 5. Delete the line matching the string
Delete the line containing "cn 11"

[root@zaishu zaishu]# sed '/cn 11/d' sedtxt2
This is zaishu.cn 1.
This is zaishu.cn 2.
This is zaishu.cn 3.
This is zaishu.cn 4.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 12.
This is zaishu.cn 13.
This is zaishu.cn 14.

Example 7. Matching rows through regular expressions
Match the line at the end of 13

[root@zaishu zaishu]# sed '/13\.$/d' sedtxt2   
This is zaishu.cn 1.
This is zaishu.cn 2.
This is zaishu.cn 3.
This is zaishu.cn 4.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 11.
This is zaishu.cn 12.
This is zaishu.cn 14.

3. Insert script command a/i

a: A row is appended to the specified row. i: Inserts a row before the specified row. Their grammar is the same.
Syntax:
sed [address]/i(a) \ contents / / append or insert the matching rows within the specified range.
Example 1. Insert / append to line number

[root@zaishu zaishu]# sed '3i\asdfasd' sedtxt2
This is zaishu.cn 1.
This is zaishu.cn 2.
asdfasd
This is zaishu.cn 3.
This is zaishu.cn 4.
[root@zaishu zaishu]# sed '3a\asdfasd' sedtxt2
This is zaishu.cn 1.
This is zaishu.cn 2.
This is zaishu.cn 3.
asdfasd
This is zaishu.cn 4.

Example 2. Insert / append multiple lines

[root@zaishu zaishu]# sed '3i\
> this is 1 \
> is 2' sedtxt2

This is zaishu.cn 1.
This is zaishu.cn 2.
this is 1 
is 2
This is zaishu.cn 3.
This is zaishu.cn 4.

Example 3. Insert or append rows through regular expression matching

Found contains welcome Rows, append xxx Content of
[root@zaishu zaishu]# sed '/welcome/a\xxx' sedtxt
hello sed
welcome to zaishu.cn welcome to zaishu.cn
xxx
welcome to zaishu.cn welcome to zaishu.cn
xxx
Found contains sed End line, append
[root@zaishu zaishu]# sed '/sed$/a\xxx' sedtxt
hello sed
xxx
welcome to zaishu.cn welcome to zaishu.cn
welcome to zaishu.cn welcome to zaishu.cn

4. Line replacement script command c

grammar. //Append or insert the matched rows within the specified range.
sed  [address]/c\content

Example 1. Replace the contents of the line

[root@zaishu zaishu]# sed '4c\change' sedtxt2
This is zaishu.cn 1.
This is zaishu.cn 2.
This is zaishu.cn 3.
change
This is zaishu.cn 5.

Example 3. Replace a row by matching it with a regular expression

Match 2. At the end of the line, replace it with change
[root@zaishu zaishu]# sed '/2\.$/c\change' sedtxt2
This is zaishu.cn 1.
change
This is zaishu.cn 3.
This is zaishu.cn 4.
This is zaishu.cn 5.
This is zaishu.cn 6.
This is zaishu.cn 7.
This is zaishu.cn 8.
This is zaishu.cn 9.
This is zaishu.cn 10.
This is zaishu.cn 11.
change

4. Character replacement script y

y is required to process a single character in the sed script command
Syntax: sed [address]//y/in/ot/
Within the specified range, replace the contents of the matched line with characters.
y will map in and ot values one-to-one, and i will be replaced by o; Replace n with t. This process continues until all characters are processed. If the lengths of in and OT are different, an error will be reported when sed is specified.

Example 1. Replace all contents in the full text, replace. With $and replace the letter e with a

[root@zaishu zaishu]# sed 'y/.e/$a/' sedtxt
hallo sad
walcoma to zaishu$cn walcoma to zaishu$cn
walcoma to zaishu$cn walcoma to zaishu$cn

Example 2. Replace the output content

Replace 1 with 2 and 2 with 4

[root@zaishu zaishu]# echo "2 is 1+1  " | sed 'y/12/24/' 
4 is 2+2  

Example 3. Replace the character of the specified line number

Replace only the characters on the second line
[root@zaishu zaishu]# sed '2y/.e/$a/' sedtxt
hello sed
walcoma to zaishu$cn walcoma to zaishu$cn
welcome to zaishu.cn welcome to zaishu.cn

Example 4. Specifying rows through regular expressions
Lines containing the string welcome, which are character replaced.

[root@zaishu zaishu]# sed '/welcome/y/.e/$a/' sedtxt
hello sed
walcoma to zaishu$cn walcoma to zaishu$cn  //Change. e into $a
walcoma to zaishu$cn walcoma to zaishu$cn

5. Save content script w

Write the contents of the specified line in the text to the file. The syntax of this command is as follows:
[address]/w filename

Example 1. Save the full text to another text

[root@prometheus zaishu]# sed 'w test2' test
abchello world
abc helloabc abc
abc abchelloabc abc
edf
ghi
dba hello
[root@prometheus zaishu]# cat test2 
abchello world
abc helloabc abc
abc abchelloabc abc
edf
ghi
dba hello
 perhaps-n Only save text as, no output action
[root@prometheus zaishu]# sed -n 'w test3' test

Example 2. Specify line number

Save the contents of 1-3 to other text

[root@prometheus zaishu]# sed '1,3w test4' test
abchello world
abc helloabc abc
abc abchelloabc abc
edf
ghi
dba hello
[root@prometheus zaishu]# cat test4 
abchello world
abc helloabc abc
abc abchelloabc abc

Example 3. Regular expression matching line

Will take abc Save the first line to a text
**[root@prometheus zaishu]# sed '/^abc/w abc.txt' test
abchello world
abc helloabc abc
abc abchelloabc abc
edf
ghi
dba hello
[root@prometheus zaishu]# cat abc.txt 
abchello world
abc helloabc abc
abc abchelloabc abc**

6. Insert additional text r

Inserts the data of a file into the current specified location.
Syntax:
[address]/r filename
Inserts the contents of the file after the specified line.

Example 1. Insert content after specifying a line

[root@prometheus zaishu]# cat append.txt 
add line
2 add line
[root@prometheus zaishu]# sed '2r append.txt' abc.txt
abchello world
abc helloabc abc
add line
2 add line
abc abchelloabc abc

Example 2. Add to last line
The data in the specified file is inserted at the end of the text, using the $address character

[root@prometheus zaishu]# sed '$r append.txt' abc.txt 
abchello world
abc helloabc abc
abc abchelloabc abc
add line
2 add line

Example 3. Regular expression matching line
Insert this text below all lines beginning with abc

[root@prometheus zaishu]# sed '/^abc/r append.txt' abc.txt
abchello world
add line
2 add line
abc helloabc abc
add line
2 add line
abc abchelloabc abc
add line
2 add line

6. Interrupt exit script command q

Q command: when q is encountered, it means to exit the sed program. Before exiting, the p command is used to output the data in the mode buffer and empty the mode buffer.
Syntax:
[address]/q

Example 1. Specify line number to exit

Output the first two lines and exit the third line sed program
[root@prometheus zaishu]# sed '2 q' abc.txt
abchello world
abc helloabc abc

Example 2. Exit line by regular expression matching

Output the line at the end of world, and exit the sed program on the next line

[root@prometheus zaishu]# sed '/world$/q' abc.txt
abchello world

Address in script command [address]

[address] script command
When introducing script commands, the use of address is not explained in detail. For the above script commands, you can use address. Select the specified range for sed operation. If address is not used, the default is full-text.

There are three methods of address representation:

  1. Specifies the row interval in numeric form
Specifies the row interval in numeric form
n	n Is the line number
m,n	Indicates that the line number is from m reach n
  1. Specify a specific line interval in text mode, and support regular expression matching.
/pattern	Query rows containing patterns
/pattern /pattern	The query contains rows of two patterns
pattern/,x	Queries the row containing the pattern on a given row number
  1. regular expression
    The syntax of regular expressions can refer to the previous section, and the address also supports matching the rows to be processed through regular expressions
x,y!	For example, the query does not contain the specified line number x and y Line of

Example 1. Specify line number

Modify the first line specified by the address.

[root@prometheus zaishu]# sed '1s/hello/hi/' abc.txt 
abchi world
abc helloabc abc
abc abchelloabc abc

Example 2. Specify multiple rows

Use the row address range to modify rows 1 and 2
[root@prometheus zaishu]# sed '1,2s/hello/hi/' abc.txt 
abchi world
abc hiabc abc
abc abchelloabc abc

**Example 3. Specify to the end of the document**
$: indicates the end. The following indicates from line 3 to the end of the document.

[root@prometheus zaishu]# sed '3,$s/hello/hi/' test
abchello world
abc helloabc abc
abc abchiabc abc
edf
ghi
dba hi

Example 4. Specify line interval in text mode
The address can filter the specified line in text mode: / pattern/command
Replace the contents of the line containing prometheus.

[root@prometheus zaishu]# grep 'prometheus' /etc/passwd
prometheus:x:998:996::/home/prometheus:/bin/bash
[root@prometheus zaishu]# sed '/prometheus/s/bin/sbin/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
..
prometheus:x:998:996::/home/prometheus:/sbin/bash
..

regular expression

sed allows complex data matching using regular expressions in [address].

[root@zaishu zaishu]# cat sedtxt
hello sed
#welcome to zaishu.cn welcome to zaishu.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

welcome to zaishu.cn welcome to zaishu.cn

Example 1. Print lines that do not begin with #

[root@zaishu zaishu]# sed -n '/^#/p' sedtxt  //Output lines beginning with #
#welcome to zaishu.cn welcome to zaishu.cn


[root@zaishu zaishu]# sed -n '/^#/!p' sedtxt  //Print lines that do not begin with #
hello sed
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

welcome to zaishu.cn welcome to zaishu.cn

Example 2. Print non blank lines

[root@zaishu zaishu]# sed -n '/^$/p' sedtxt / / print blank lines

[root@zaishu zaishu]# sed -n '/^$/!p' sedtxt 	// Print non blank lines
hello sed
#welcome to zaishu.cn welcome to zaishu.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
welcome to zaishu.cn welcome to zaishu.cn

Example 3. Multi level matching: in these lines that do not start with # and are not empty, replace nologin with login

First, the contents beginning with # cannot be found, then remove the blank line from these contents, and then replace these contents.

[root@zaishu zaishu]# sed '/^#/!{/^$/!s/nologin/login/}' sedtxt
hello sed
#welcome to zaishu.cn welcome to zaishu.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/login
nobody:x:99:99:Nobody:/:/sbin/login

welcome to zaishu.cn welcome to zaishu.cn

sed [options]

[ root@zaishu ~]#Sed [options] [script command] file name

optionmeaning
-e script commandThis option adds the script command that follows it to the existing command.
-f script command fileThis option adds script commands from subsequent files to existing commands.
-nBy default, sed will automatically output the processed content after all specified scripts are executed, and this option will mask the startup output. You need to use the print command to complete the output.
-iThis option modifies the source file directly
-r : Syntax that supports extended normal representations. In short, if you want to use less in regular expressions, add - r

This chapter focuses on [options]

1.sed -i option

sed -i is to modify the original file directly. This option is used very frequently in work. Previously, when talking about sed, we only modified the data in the buffer, and the data in the original file will not be changed.

View the contents of the original file

[root@zaishu zaishu]# cat sedtxt
hello sed
#welcome to zaishu.cn welcome to zaishu.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

welcome to zaishu.cn welcome to zaishu.cn

Modify the line of the original file

[root@zaishu zaishu]# Sed - I's / zaishu / zs / g 'sedtxt / / replace zaishu with zs
[root@zaishu zaishu]# cat sedtxt
hello sed
#welcome to zs.cn welcome to zs.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
welcome to zs.cn welcome to zs.cn

Delete line of original file

[root@zaishu zaishu]# sed -i '2d' sedtxt / / delete the second line
[root@zaishu zaishu]# cat sedtxt
hello sed
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
welcome to zs.cn welcome to zs.cn

**Replace characters in the specified file**

[root@zaishu zaishu]# Sed - I '3Y / Y: / A & /' sedtxt / / replace y in the third line of text with a:& 
[root@zaishu zaishu]# cat sedtxt
hello sed
ftp:x:14:50:FTP Usar:/var/ftp:/sbin/nologin
noboda&x&99&99&Noboda&/&/sbin/nologin
welcome to zs.cn welcome to zs.cn

2. sed -e option

When there are multiple command scripts, you can use - e to specify multiple command scripts.

-e : You can execute multiple commands on the same line
[root@zaishu zaishu]# sed -e 's/zaishu/zs/g' -e 's/nologin/login/g' sedtxt
hello sed
#welcome to zs.cn welcome to zs.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/login
nobody:x:99:99:Nobody:/:/sbin/login

welcome to zs.cn welcome to zs.cn

Delete lines beginning with hello and blank lines

[root@zaishu zaishu]# sed -e '/^hello/d' -e '/^$/d' sedtxt
#welcome to zaishu.cn welcome to zaishu.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
welcome to zaishu.cn welcome to zaishu.cn

3.sed -f option

sed -f this option is used to put the script command into a separate file, and sed executes the script command that calls this file as it. This option is useful when script commands are very complex.

[root@zaishu zaishu]# cat sedtxt
hello sed
#welcome to zaishu.cn welcome to zaishu.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

welcome to zaishu.cn welcome to zaishu.cn
  1. Edit rule file
[root@zaishu zaishu]# cat rule.sed 
s/zaishu/zs/g
s/nologin/&gin/g
  1. Call the rule file as a script command
[root@zaishu zaishu]# sed -f rule.sed  sedtxt
hello sed
#welcome to zs.cn welcome to zs.cn
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologingin
nobody:x:99:99:Nobody:/:/sbin/nologingin

welcome to zs.cn welcome to zs.cn

4.sed -r option

sed -r supports the syntax of extended normal notation. In short, if you want to use less in regular expressions, add - r
For example, replace the numbers 10-19 with 1

[root@zaishu zaishu]# Echo 192.168.12.13|sed's / 1 \ + \ ([0-9] \ + \) / \ 1 / g '/ / not applicable - r
92.68.2.3
[root@zaishu zaishu]# Echo 192.168.12.13|sed - r's / 1 + ([0-9] +) / \ 1 / g '/ / use - r
92.68.2.3

-r this option can be understood, depending on personal habits

summary

This section explains the usage of sed in detail, combined with the regular expressions learned before. This article is completely from Xiaobai's point of view. It took a long time to edit it so that everyone can understand and operate it as much as possible. Text processing also has the most powerful tool awk, which will be explained in a separate album.

Keywords: Linux bash regex

Added by clarket on Sat, 06 Nov 2021 08:57:03 +0200