1. Introduction to SED
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The contents of the file do not change unless you use the redirect store output or with the edit option. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on. The GNU version of SED 3.02 is described below.
2.SED operating mode
Read a line of content-edit this line-and output the edited result, and empty the pattern space-and read the next line of content
3. Usage: sed OPTIONS ... [SCRIPT] [Inputfile ...]
-R: Using extended regular expressions;
Example: # Sed-n-R '/^ (dev|tmp)/P '/tmp/fstab.txt
-N,--quiet,--silent: Does not output the contents of the mode space; "That is: the output is matched to the content"
Example: # sed-n ' 1,3p '/etc/fstab
# sed-n ' 1,3d '/etc/fstab
-I: Directly edit the original file;
Example: # sed-i ' 3,7d '/tmp/fstab.txt
-E:-E '-e ' '-e '
' CMD1; CMD2; CMD3 '
Example: # sed-e ' s/uuid/uuid/g '-e ' s/proc/proc/g '/tmp/fstab.txt
-f/path/to/sed_script: Read processing script from the specified file
For example:
# Vim Sed.script
S/uuid/uuid/g
S/proc/proc/g
3d
# sed-f Sed.script/tmp/fstab.txt
4. Address delimitation:
You can locate the row you want to edit by addressing it, which is made up of numbers, and two rows separated by commas represent the range of rows starting and ending with these two behaviors (including the two lines represented by the number of rows). The dollar sign ($) represents the last line, as 1,3 represents a one-line. The range can be determined by data, regular expressions, or by combining them.
The specific usage is as follows:
#: Specify Line
Example: # sed ' 10d '/etc/fstab
Validation comparison: # Cat-n/etc/fstab
$: Last line
Example: # sed ' $d '/etc/fstab
/regexp/: Any row that can be matched to a regexp
Example: Remove lines beginning with #: # sed '/^#/d '/etc/fstab
\%regexp%: Any line that can be matched to the regexp, and the% as the boundary symbol;
Example: # sed '/^\/dev\/mapper/d '/etc/fstab
or # sed ' \@^/dev/mapper/@d '/etc/fstab
or # sed ' \%^/dev/mapper/%d '/etc/fstab
/regexp/i
\%regexp%i: Ignoring character case when matching;
ADDR1, ADDR2: All rows in the specified range:
0,/regexp/
#,#
/regexp/,/regexp/
#,+#
First~step: Specify the starting position, and the step size
5. Basic regular Expression meta-characters:
Character:. [] [^]
Number of times: *, \+, \?, \{i,j\}
Group: \ (\)
Anchoring: ^, $, \<, \>
Or: \|
Back reference: \1, \2, ...
6. Edit the command:
Note: The command can be used before! indicates the reverse operation;
=: Show line number "line number of the line being matched to"
# sed-n ' \%/dev/mapper%= '/etc/fstab
# sed-n ' \/^[[:upper:]]/= '/etc/fstab
D: Delete the matching rows;
P: Print the contents of the mode space;
For example: # sed ' 1,3p '/etc/fstab "1-3 rows Repeat, because the default 1-3 is printed once, plus the P command, printing again, so it repeats"
Usually used with the-n option;
# sed-n ' 1,3p '/etc/fstab
A \text:append,
A \text1\ntext2\ntext3: Append multiple lines of text
Example: # sed '/^[[:upper:]]/a \# upper letters '/etc/fstab
# sed '/^[[:upper:]]/a \# upper letters\n# Test text '/etc/fstab
I \text:insert,
I \text1\ntext2: Insert multiline text before insert to line to match
Example: # sed '/^[[:upper:]]/i \# upper letters\n# Test text '/etc/fstab
C \text:change, replacing the matched line with the given text
Example: # sed ' 3c \substitue text '/etc/fstab
s///: Replace command
# sed ' s\uuid\uuid\ '/etc/fstab
G: Global Substitution
I: Case insensitive
P: Print If successful replacement
W/path/to/somefile: Saves the replaced result to the specified file
Example: # sed ' s/tmpfs/tmpfs/gw/tmp/sed.txt '/etc/fstab
# Cat/tmp/sed.txt
W/path/to/somefile: Save match to file in specified file
Example: # sed ' 3,7w/tmp/fstab.txt '/etc/fstab
# Cat/tmp/fstab.txt
R/path/from/somefile: Reads the specified file to the line where it is matched
For example, after inserting a/etc/issue into the UUID
# sed ' \/uuid/r/etc/issue '/etc/fstab
7. Practice
Exercise 1: Delete the blank at the beginning of the line in the/boot/grub/grub.conf file so the whitespace begins;
Sed ' s/^[[:space:]]\+//g '/boot/grub/grub.conf
Exercise 2: Remove all # and white-space characters from the beginning of the line at the beginning of #, followed by at least one white-space character, in the/etc/fstab file;
Sed ' s/^#[[:space:]]\+//'/etc/fstab
Practice 3:echo A file path to the SED command, take out its base name, and take out its directory name;
# echo "/etc/sysconfig/" | Sed ' [Email protected][^/]\{1,\}/\[email protected]@g '
Parse: [^/]\{1,\} appears at least once for non-slash characters
Exercise 4: Replace the/etc/inittab file "ID: Number: Initdefault:" The number in the row is 6;
# sed ' [email protected]:[0-9]:initdefault: @id: 6:[email protected] '/etc/inittab
8. Advanced Editing Commands:
H: Use the content in the pattern space to keep the content in the space;
H: The content of the pattern space to pursue to maintain space;
G:get, retrieving data from the hold space to the mode space, overwriting the original in the mode space
G:get, retrieving data from the hold space to the mode space, appended to the mode space, now
X: Swap The pattern space and the content in the hold space;
N: Reads the row below the line to the pattern space;
N: Append read matches to the next line to the pattern space;
D: Delete the contents of the pattern space;
D: Delete the first row in multi-line mode space;
9. Common usage "advanced command"
Sed ' 1! G;h;$!d ' FILE
Show even lines of files: Sed-n ' n;p ' file; Sed-n ' 2~2p ' FILE
1
2
3
4
Displays odd lines of files: Sed-n ' p;n ' file; Sed-n ' 1~2;p ' FILE
10. Advanced Command Practice
Exercise 1: Use SED to convert odd and even lines of a file
# sed ' $! N [Email protected]\[email protected]\&\&@g '/etc/issu
Exercise 2: Illustrate the meaning of the following SED commands:
(1) SED ' $! n;$! D '/etc/issue
(2) Sed ' $!d '/etc/issue
(3) Sed ' G '/etc/issue
(4) sed '/^$/d; G '/etc/issue
(5) Sed ' n;d '/etc/issue
(6) Sed-n ' 1! G;h, $p '/etc/issue
This article is from the "West to Dragon Elephant" blog, please make sure to keep this source http://burgess8909.blog.51cto.com/9607271/1666146
SED command detailed