I. INTRODUCTION of SED
Role: In the Behavior unit processing, the data can be replaced, deleted, added, selected and other specific work.
Format: sed + parameters + ' function ' + file
sed-n ' p/etc/passwd Print line 10th Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Ii. sed parameters
-N cancels the default output, using quiet (silent) mode. In the usage of general sed, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed. Note: the-n parameter is used with p, otherwise just-n will not have any output
-R is similar to grep's-e parameter, making the SED command used? , +, {}, and so on, do not need to precede these meta-character expansion sets with escape characters
-I Direct Action source file, the source file will be modified. (Note is lowercase i, not l)
-e multiple edits, which are used when multiple SED commands are applied to an input line
sed-n '/d\? s/' P 1.txt //Do not use the-R parameter to add an escape character netmask=255.255.255.0dns1=192.168.1.1dns2=8.8.8.8
sed-n '/d? s/' P 1.txt–r //using the-R parameter does not require an escape character netmask=255.255.255.0dns1=192.168.1.1dns2=8.8.8.8
Third, sed function
P print lines, used with the-n parameter
D Delete
s substitution character (in conjunction with G to represent global substitution)
I insert text before the current line
A adds one or more rows after the current line
Extension: Exercises 1
Print line 10th: sed-n ' p filename
Print 1-10 lines: sed-n ' 1,10 ' p filename
Print 20 lines to the last line: Sed-n ' 20,$ ' p filename
Print the line containing ABC: sed-n '/abc/' p filename
Delete blank line to display: Sed-n '/^$/' d filename
Delete the first 3 lines to show: sed ' 1,3 ' d filename
Delete 1-10 lines to show: sed ' 1,10 ' d filename
Delete the row containing root to display: sed '/root/' d filename
Delete rows containing numbers to display: sed '/[0-9]/' d filename
Delete the line containing the letter to display: sed '/[a-za-z]/' d filename
Lines with letters in the first 5 rows Delete: head-n 5 filename | Sed '/[a-za-z]/' d
Replace all numbers with empty: sed ' s/[0-9]//g ' filename
Replace all letters with empty: sed ' s/[a-za-z]//g ' filename
Replace all non-numeric, non-alphabetic substitutions with null: sed ' s/[^0-9a-za-z]//g ' filename
Replace the line containing def with ABC: sed ' s/.*def.*/abc/g ' filename
Insert title before first line welcome:sed ' 1iwelcome ' filename
Add the end:sed ' $aTHE end ' filename at the end of the file
Extension: Exercises 2
1. Copy/etc/passwd to/root/test.txt, print all lines with SED
2. Print 3 to 10 lines of test.txt
3. Print the line containing ' root ' in the Test.txt
4. Delete the 15 lines of test.txt and all subsequent rows
5. Delete the line containing ' bash ' in Test.txt
6. Replace ' root ' as ' Toor ' in Test.txt
7. Replace Test.txt '/sbin/nologin ' as '/bin/login '
8. Delete all numbers from rows 5 to 10 in Test.txt
9. Delete all special characters in test.txt (except for numbers and uppercase and lowercase letters)
10. Swap the first and last words in the test.txt for a position
11. Replace the first and last words that appear in the test.txt with the position
12. Move the first number in the Test.txt to the end of the line
13. Add ' AAA: ' At the front of the Test.txt 20 line to the last line
Answers to the exercises:
1./bin/cp/etc/passwd/root/test.txt; Sed-n ' 1,$ ' P test.txt
2.sed-n ' 3,10 ' P test.txt
3.sed-n '/root/' P test.txt
4.sed ' 15,$ ' d test.txt
5.sed '/bash/' d test.txt
6.sed ' s/root/toor/g ' test.txt
7.sed ' S#sbin/nologin#bin/login#g ' test.txt
8.sed ' 5,10s/[0-9]//g ' test.txt
9.sed ' s/[^0-9a-za-z]//g ' test.txt
10.sed ' s/\ (^[a-za-z][a-za-z]*\) \ ([^a-za-z].*\) \ ([^a-za-z]\) \ ([a-za-z][a-za-z]*$\]/\4\2\3\1/' test.txt
11.sed ' s#\ ([^0-9][^0-9]*\) \ ([0-9][0-9]*\) \ ([^0-9].*\) \ ([^a-za-z]\] \ ([a-za-z][a-za-z]*$\) #\1\5\3\4\2# ' Test.txt
12.sed ' s#\ ([^0-9][^0-9]*\) \ ([0-9][0-9]*\) \ ([^0-9].*$\] #\1\3\2# ' test.txt
13.sed ', $s/^.*$/aaa:&/' stest.txt
Regular Expressions: sed