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
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
2, print odd-numbered lines passwd file
Sed -n “1~2p” passwd
3, starting from the first row, every five lines of print about the contents of the file passwd
4, print passwd file contains rows of root
5, the print file to the user passwdroot all lines of mail between users
6, the last line of print passwd file
Second, the master and the " OK " instructions related to - ( D , C , A , I )
1, the first line of the passwd file deletion
2, delete the file passwd in the first five
3, delete the file passwd even lines
4, users can not delete files passwd login
5, delete files dhcp in the blank lines and comment lines
6, the UUID to the line in the file ens "HWADDR = 00: 00: 00: 12: 34: 56"
7, added to the end of the ens "DNS1 = 8.8.8.8"
8, add a blank line after each line of ens
9, add comments in the above ens in PREFIX line "# you can usr NETMASK instead of PREFIX"
Three, alternative instruction master -s
1, the first root passwd file appear in each row replace admin
2, all the root passwd file appear in each row replace admin
3, the separator passwd file: to space
4, delete files ens first character of each line
5, delete files ens second character per line
6, delete files ens penultimate character
7, delete files group all digital
8, the swap file ens first character of each line and the second character
9, the display of the first field passwd
16, the display passwd first field and a last field, with tabs spaced intermediate
Reproduced in: https: //blog.51cto.com/14015496/2411227