SED: Data Flow Editor
Reads a row into the memory processing row and then outputs a row.
Mode space:
SED: Do not edit source files by default
sed [option] Addresscommand file
1, start line, end line
sed startline,endline file sed 2,5p file
2. Pattern matching/mode/
Sed/^root/p file matches to print two times, because each of itself is printed
3,/mode 1/,/mode 2/
Sed/^l/,/^h/p file begins with a line of L to a line ending with H, meaning the first matching
4, give only a numeric value, line number, indicating the precise designation of a row
Sed 5p file
5, specify the starting line, using +n, to start from the specified line, the backward N rows, a total of n+1 line content
Sed 10,+5 file
SED command:
P: Show rows that match the criteria
D: Delete qualifying line sed/^ '/d file delete line beginning with whitespace
A:append usage: A \string appends a new line after the specified line with the contents of string
# sed '/d$/a \hello world\nhello World ' file adds two lines after ending with D
In fact, the content as a string appended, just \ n in the text is treated as a newline character
I:insert usage: I \string insert a new line before the specified line. Content is string \ n
R:r filename Adds the contents of the specified file to the qualifying line at sed '/d$/r/root/t2 ' t
W:w filename Saves the specified range of content to the specified file (repeated overrides) Sed/d$/w/tmp/d.txt T
S: Find and replace. s/Match mode/content to replace/;@ # $% A
Each line is replaced with all matching items:
G: Global replacement sed s/d/d/g file sed/s/l. e/&r/g file & refers to the whole of the match
I: Ignoring the case of characters when looking for
Options for SED:
-N: Static mode, does not display the contents of the pattern space, only matches the line of the condition sed-n 1p file
-R: Use extended regular expression sed-r s/(L.. e) (m+)/\1r\2xx/g file \1 refers to the whole (back reference) in parentheses without escaping parentheses
-I: Modify source files directly
Practice:
There is a text file:
Username,age,gender,salary
zhangsan,20,f,2000
A back reference to SED:
& represents the entire string for a reference pattern match
Practice:
1. Delete the blank character from the beginning of the result of the history command [[: Space:]]
History | Sed s/^[[:space:]]//g
2. Take out the parent directory of a file path such as/var/log/mysql/acces.log ===>/var/log/mysql/
echo "/var/log/mysql" | Sed-r ' s#^ (/?. */). +/?#\1#g '
3. Delete files with white space characters + #开头的行中的空白字符及 #
#asdsdasd
==>
Asdasd
Sed-r s/^[[:space:]]+#//g File
Sed-r ' s/^ (l.*e$)/#\1/g ' file add comment to L ending with E
shell--Learning SED