Overview of the SED tools (streaming editor)
Non-interactive, filter and modify text based on pattern matching
Line by row and output the results to the screen
can achieve the output of the text, increase, delete, change, check and other operations
Sed flow control:
!: Take counter action, reverse according to addressing condition
N: Reads the next line, produces interlaced, skip effect
Command format parsing:
Format one: sed [option] ' edit directive ' file
Format two: Pre-command |sed [option] ' Edit Command '
Common Command options:
-N: Mask default output
-I: Modify file contents directly
-F: Using SED scripts
-E: Multiple processing actions can be specified
-R: Enable extended regular expressions, if used with other options, should be a preference
-{}: Multiple commands can be combined, separated by semicolons
Edit instruction Specify the address character
addressable character, i.e. [address 1],[address 2]
Used to specify the number of stops for processing
Omit a custom character, the default line-by-row processing of all text
Addresses can be represented as "line numbers" of text or "/Regular expressions/" used to match
Basic processing of SED
____________________________________________________________________________________+
|
Example of operator Usage directives |
P Print line 2,4 output 2,3,4 line 2p;4p output line 2nd, line 4 |
d Delete Row 2,4d Delete line 2,3,4 |
s string Substitution s/old/new replaces the first old of each line with new, |
S/OLD/NEW/3 Replace the third old of each row with new, |
s/old/new/g replace all old with new |
____________________________________________________________________________________+
The separation of the replace operation/can be used instead of other characters such as # &, easy to modify the file path
Text Processing for SED
——————————————————————————————————————————
Example of operator purpose directives
Insert text before line I 2iyy add a line of text before the second line yy
4,7iyy adding lines of text before each line of the 第4-7 line
Insert text after line a 2ayy add text after line 2nd
/^xx/ayy adding text after a line beginning with XX
C replaces the current line 2CYY modifies the contents of line 2nd to YY
——————————————————————————————————————————
Using SED scripts
Complex operations can be saved as scripts, called by-F
Format: sed-f Action script file
[Email protected] ~]# vim/root/test.sed
s/pang/&jing/
S/[0-9]//g
[Email protected] ~]# sed-f/root/test.sed a.txt
Example:
[[Email protected] ~]# sed '/ipaddr/c ipaddr=192.168.4.40 '/etc/sysconfig/network-scripts/ifcfg-eth0 (modify IP)
[[Email protected] ~]# sed ' 2i xxx ' a.txt (added)
1
Xxx
2
3
[[Email protected] ~]# sed ' 1,3d ' a.txt (delete 1 lines 3 lines)
4
5
6
[[Email protected] ~]# sed ' s/4/xx/1 ' a.txt (replace)
1
2
3
Xx
5
6
[Email protected] ~]# sed-r ' s/(...) .... (...) /\1***\2 ' A.txt
File Import and Export
Basic action:
-R: Action should be combined with-I option will not be deposited, otherwise only output
-W: Action to Save as new file in overwrite mode
Example
[[Email protected] ~]# sed ' 3r b.txt ' a.txt Insert file below the third line B.txt
[[Email protected] ~]# sed ' 4,7r b.txt ' a.txt Insert file after 第4-7 per line B.txt
[[Email protected] ~]# sed ' 3w c.txt ' a.txt Save the third row as C.txt
SED copy cut
Mode space
Stores the currently processed row, processing the result output
If the current row does not conform to the processing condition, the output is
The current line is processed and then read into the next line to process
Keep space
function similar to shear plate
A blank line (newline character \ n) is stored by default.
Basic action:
-h; Mode space--[append]--"Keep space
-h; Mode space--[cover]--"Keep space
H,h ——— Replication
-G: Keep space--[append]--"mode space
-G: Keep space--[overlay]--"mode space
g,g--paste
————————————————————————————————————————————————————————
Shell Script sed tool