sed command

1. a stream editing tool for replacement of the text filtering and

2. The first reading the contents of a line for processing certain instructions

3. Default without directly modifying the source file, but copying the contents to be processed and spatial mode output, the default output to standard output (i.e., screen)

sed basic syntax

image

Options and parameters:
-n: Use quiet (silent) mode. Sed in general usage, all data from STDIN generally will be listed to the terminal. However, if after adding -n parameter, only through the line sed special treatment (or action) will be listed.
-e: edit sed operates directly on the command-line mode;
-f: sed directly to the action in a written document, -f filename you can run sed action in the filename; -r: sed action is supported by regular expression syntax type of extension. (The default is the basis for formal notation syntax)
-i: directly modify the contents of the file to read, and not output to the terminal.
 
Action Description: [n1 [, n2]] function
n1, n2: not necessarily exist, usually on behalf of "the number of rows selected for action", for example, if my action is needed between 10-20 row, "10, 20 [action behavior]."
function:
a: new, can take back a string, and those strings come (currently the next row) in a new line ~
c: substituted, may be connected to the back c string, these strings can be substituted n1, N2 between the line!
d: delete, because it is deleted ah, so the back d usually do not take any character;
i: insert, i can take back the string, which string appears (the line current) is a new line;
p: print, that will print a selection of data. P will usually run together with the parameters sed -n ~
s: substitution can be substituted directly work! Usually this s action can be used with regular expression! E.g. 1,20s / old / new / g
 

First, the master address representation - (binding options n- , instructions = and P )

1, print passwd file first two lines

However -n "1,2 p" passwd

clip_image004

2, print odd-numbered lines passwd file

Sed -n “1~2p” passwd

clip_image006

3, starting from the first row, every five lines of print about the contents of the file passwd

clip_image008

4, print passwd file contains rows of root

clip_image010

5, the print file to the user passwdroot all lines of mail between users

clip_image012

6, the last line of print passwd file

clip_image014

Second, the master and the " OK " instructions related to - ( D , C , A , I )

1, the first line of the passwd file deletion

clip_image016

2, delete the file passwd in the first five

clip_image018

3, delete the file passwd even lines

clip_image020

4, users can not delete files passwd login

clip_image022

5, delete files dhcp in the blank lines and comment lines

clip_image024

6, the UUID to the line in the file ens "HWADDR = 00: 00: 00: 12: 34: 56"

clip_image026

7, added to the end of the ens "DNS1 = 8.8.8.8"

clip_image028

8, add a blank line after each line of ens

clip_image030

9, add comments in the above ens in PREFIX line "# you can usr NETMASK instead of PREFIX"

clip_image032

Three, alternative instruction master -s

1, the first root passwd file appear in each row replace admin

clip_image034

2, all the root passwd file appear in each row replace admin

clip_image036

3, the separator passwd file: to space

clip_image038

4, delete files ens first character of each line

clip_image040

5, delete files ens second character per line

clip_image042

6, delete files ens penultimate character

clip_image044

7, delete files group all digital

clip_image046

8, the swap file ens first character of each line and the second character

clip_image048

9, the display of the first field passwd

clip_image050

16, the display passwd first field and a last field, with tabs spaced intermediate

clip_image052