650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/9C/wKiom1ep2zny4PdwAAFu9IzGW3E832.jpg "title=" SED working mode. jpg "alt=" wkiom1ep2zny4pdwaafu9izgw3e832.jpg "/>
SED works: sed reads a line from a file, does not directly edit the processing, but will be placed in its own mode space for processing, the results will be sent to the standard output, by default, SED can process each line of text, you can also let sed processing the qualifying rows, We can use regular expressions to do text filtering, pattern space has two output results, one can be matched to, according to the editing requirements of processing, such as delete, filter string. The other is that the match fails and the standard output is done directly.
Sed common options:
-N: does not output the contents of the mode space to the screen, and does not output the information in the pattern space.
-e: Multi-point editing, multiple matching criteria can be edited at the same time
-f:/path/sed_script_file one edit command per line; Read filter conditions within a file
-R: Supports the use of extended expressions; use a bitter expression
-I: Directly edit the source file, the operation is more dangerous, will change the original file directly.
Address delimitation:
(1) Empty address: To deal with the full text;
(2) Single address:
#: Specify line;
[Email protected] ~]# sed-n ' 2p '/etc/passwd
/pattern/; Each row that is matched by this pattern;
[Email protected] ~]# sed-n '/^root/p '/etc/passwd
(3) Address range:
#,#:
[Email protected] ~]# sed-n ' 1,2p '/etc/passwd
#,+#: Backwards + #行
[Email protected] ~]# sed-n ' 1,+2p '/etc/passwd
#,/pat1/:
[Email protected] ~]# sed-n ' 1,/^nologin/p '/etc/passwd
/pat1/,/pat2/
[Email protected] ~]# sed-n '/^root/,/^nologin/p '/etc/passwd
(4) Step forward: ~
: All odd lines
[Email protected] nzg]# sed-n ' 1~2p ' F1
2~2: All even lines
[Email protected] nzg]# sed-n ' 2~2p ' F1
Edit command:
D: delete;
[[Email protected] nzg]# sed ' 2~2d ' f1 delete even rows
P: Displays the contents of the mode space;
[[email protected] nzg]# sed-n ' 2~2p ' F1 show matching results
a \text: Append the text "text" After the line, support using \ n to implement multi-row append;
[[Email protected] nzg]# sed ' 100a \luyubo ' F1
[[Email protected] nzg]# sed ' 100a \luyubo\nnaozhongge ' F1
i \text; Insert text "text" in front of line, support using \ n to implement multi-row insert; Backup can be implemented
[[Email protected] nzg]# sed ' 100i \luyubo ' F1
[[Email protected] nzg]# sed ' 100i \luyubo\nnaozhongge ' F1
C \text: Replace the matching wait line with the text specified here "text";
[[Email protected] nzg]# sed ' 100c \luyubo ' F1
[[Email protected] nzg]# sed ' 100c \luyubo\nnaozhongge ' F1
W/ptah/to/somefile: Saves the line of pattern space to the specified file;
[Email protected] nzg]# sed-n '/^[^#]/w/home/nzgqq '/etc/fstab
R/path/from/somefile: Reads the contents of the specified file to the end of the line where the current file is matched by the pattern;
[Email protected] nzg]# sed-n '/^[^#]/r/home/nzgqq '/etc/fstab
= The line number is printed for the line to which the pattern matches;
[[Email protected] nzg]# sed '/^[^#]/= '/etc/fstab
! The condition takes the reverse;
Address bound! Edit command;
[Email protected] nzg]# sed-n '/^[^#]/!p '/etc/fstab
s///: Find replacement, its delimiter can be self-specified, commonly used are [email protected]@@,s## #等;
For example, delete all whitespace characters in the/etc/grub2.conf file that start at the beginning of the line with whitespace
[Email protected] nzg]# sed-n ' [email protected]^[[:space:]]\[email protected]@p '/etc/grub2.cfg
Replace tag:
G: global substitution;
[Email protected] nzg]# sed-n ' [email protected][[:d igit:]]@[email protected] ' F1
W:/path/to/somefile; Saves the result of the replacement success to the specified file;
P: Shows the successful replacement line;
[Email protected] nzg]# sed-n ' [email protected][[:d igit:]]@[email protected]/home/f22 ' F1
Advanced Editing Commands:
H: The content of the pattern space is covered in the holding space;
H: Append the contents of the pattern space to the holding space;
G: To cover the contents of the holding space in the pattern space;
G: Append the contents of the holding space to the pattern space;
x: Swap the content in the pattern space with the content in the hold space;
N: Overwrites the next line of the row to the pattern space in the read match;
N: Append the next line of the row to the pattern space to read;
D: Delete rows in the pattern space;
D: Delete all rows in multi-line mode space;
Examples of advanced editing commands:
Sed-n ' n;p ' FILE displays even numbers
Sed ' 1! G;h;$!d ' FILE reverse Display
Sed ' $! n;$! D ' FILE shows the last two lines
Sed ' $!d ' FILE displays the last 1 rows
Sed ' G ' FILE inserts a space after each line
Sed ' g ' FILE Replace all spaces
Sed '/^$/d; G ' FILE inserts a space after each line
Sed ' n;d ' FILE shows odd numbers
Sed-n ' 1! g;h; $p ' FILE reverse Display
Examples of practice exercises:
1. Remove all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace
[[Email protected] nzg]# sed ' [email protected]^[[:space:]]\[email protected]@p '/etc/grub2.cfg
2. Remove all # and white-space characters from the beginning of the line in the/etc/fstab file that begin with #, followed by at least one white space character
[[Email protected] nzg]# sed ' [email protected]^#[[:space:]]\[email protected]@p '/etc/fstab
3. Add # At the beginning of each line of/root/install.log
[[Email protected] ~]# sed ' [email protected]^@#@ ' Install.log
4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #
[Email protected] ~]# sed-r ' [email protected]*@#&@ ' Install.log
[[Email protected] ~]# sed ' [email protected]^@#@ ' Install.log
5. Process the/etc/fstab path and use the SED command to remove its directory name and base name
[Email protected] ~]# echo "/etc/fstab" | Sed-r ' [email protected]^ (/.*/) ([^/]+/?) @\[email protected] '
[Email protected] ~]# echo "/etc/fstab" | Sed-r ' [email protected]^ (/.*/) ([^/]+/?) @\[email protected] '
6. Use SED to remove the IPv4 address of the ifconfig command
[Email protected] ~]# Ifconfig | Sed-n '/\<inet\>/p ' | Sed-r ' [email protected] ([0-9]+\.) {3} [0-9]+). *@\[email protected] '
[Email protected] ~]# Ifconfig | Sed-n '/\<inet\>/p ' |sed-r ' [email protected] @@ ' | Sed-r ' [Email protected]*@@ '
7. Statistics for all RPM files in the package directory on the CentOS installation CD. Number of repetitions of the second-to-last field
[email protected] packages]# ll *.rpm |sed-r ' [email protected]*\. ([^.] +) \[email protected]\[email protected] ' |sort |uniq-c
This article is from the "Linux Learning path" blog, so be sure to keep this source http://luyubo.blog.51cto.com/7634254/1836374
Linux Text Processing tool sed