1 sed, also known as the stream editor, reads rows from the text file into the middle of the pattern space, edits the rows that match the editing criteria, and outputs them to the monitor. The default sed does not edit the original file to handle only the contents of the mode space.
2 sed usage
sed [option] ' addresscommand[modifier] ' file
2.1 option
-N in silent mode, the so-called silent mode is to output only the rows that are matched, not the contents of the default display mode space
-I directly modify the original file
-R using extended regular expressions
-e connect multiple SED commands simultaneously
such as Sed-e ' $a \this is end '-ne '/\<if\>/p '/ETC/RC.D/RC This is the simultaneous use of two sed,
-F followed by a file (the contents of the file are all Addresscommand)
2.2 Address Search scope
1 specifying a single row for exact matching
2 Specify a row to a line such as 3,5 expression 3-5 rows
3 3,+n indicates the third row followed by N rows
4/Regular Expression/indicates the row to match in the regular expression
5/Regular Expression 1/,/Regular expression 2/indicates the first row that is matched to a regular expression 1 and the line between the rows that are matched by the regular expression 2.
2.3 Command Editing commands
D Delete Delete
P printf Printing
A \string a new row underneath the match to the line writes the contents of the string
I \string a new row on the matching line and writes
R file reads the contents of the file into a matching line
W file writes the matching line contents to the file
s/pattern/string/
S///1118.www.qixoo.qixoo.com This form of expression can also be @@@ 或者 # # # in this form
\ (\) \1 \2 can also use a reverse reference, and when the reference is global, it can also be referenced directly with &.
2.4 Modifiers
-G global match indicates that all eligible replacements in each row
-I ignores character capitalization
Practice section:
1 Remove the whitespace from the first line of/etc/grup.conf
Sed s/^[[:space:]]///etc/grub.conf
Sed ' 1, $s/^[[:space:]]//'/etc/grub.conf here Notice that there is no space directly behind S/
2 The number in "id=3" in the replacement/etc/inittab is 5
Sed ' s/id:3/id:5/'/etc/inittab
Sed ' $s/id:3/id:5/'/etc/inittab
Sed ' $s/^id:3/id:5/'/etc/inittab
3 delete blank lines in/etc/inittab
Sed '/^#$/d '/etc/inittab
4 Delete the # number at the beginning of the/etc/inittab file
Sed ' s/^#//'/etc/inittab
5 Remove a directory name from a file path
echo "/etc/rc.d/" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\
Decomposition of the above problems
1/etc/rc.d/searches for ^/.*/characters when the default greedy mode searches the entire string/etc/rc.d/
2/etc/rc.d/Search [^/]+ The greedy mode of the default search matches a word (where the word is a character set of letters and numbers) etc; if no "+" is matched to a character, [] represents a single character that matches a single character [^/] for each other. [^/]+ after ^/.*/, it means the word rc.d.
When the first part of the greedy mode and the second part of the match to the same character, the second part of the match from the beginning of the word to the previous word in the direction of matching, in the reverse reference when it narrowed the matching range at this time tightly matched only/etc//this part
3/? The ^/.*/matches the range coincident according to the minimum range matching principle, then the result of the last inverse reference match is
/etc/
7 count the number of occurrences of each word in the/etc/init.d/functions file and sort
Sed-r ' s/[^[:alpha:]]+/\n/g '/etc/init.d/functions | Sort | Uniq-c
Linux Learning Basics 6 SED usage