grep, sed (stream editor), awk (report text generation)
SED basic usage:
Sed:stream EDitor
Line Editor (full screen Editor: VI)
SED: Mode space
By default, the source file is not edited, only the data in the pattern space is processed; Then, after processing is finished, print the pattern space to the screen
-N: Silent mode, no longer displays the contents of the mode space
-I: Modify source files directly
-E script-e script: Multiple scripts can be executed at the same time
-f/path/to/sed_script
Sed-f/path/to/scripts file Multiple scripts are placed in a document
-r: Indicates the use of extended regular expressions
Address:
1. Startline,endline
Like 1,100.
$: Last line
2./regexp
/^root/
3./pattern1/,/pattern2/
The first line that is matched by the patter1 begins at the end of the line that is first matched to the pattern2, and all the rows in the middle
4. linenumber
The specified row
5. StartLine, +n
Starting from Startline, n rows backward;
Command:
D: Delete the qualifying line;
Sed ' 1,2d '/etc/fstab
Sed '/oot/d '/etc/fstab
Sed ' 1,+2d '/etc/fstab
P: Displays rows that match the criteria;
Sed '/^\//d '/etc/fatab
Sed-n '/^\//p '/etc/fstab
A/string: Appends a new row after the specified line, with the contents of string
\ n: can be used for line wrapping
Sed '/^\//a \# helloworld '/etc/fstab
Sed '/^\//a \# helloworld\n#hello,linux '/etc/fatab
I \string: Adds a new row before the specified line, with the contents of string
R file: Adds the contents of the specified file to the qualifying line
Sed ' 2r/etc/issue '/etc/fstab
W File: Save the row in the range specified by the address as the specified file;
Sed '/oot/w/etc/tmp '/etc/fstab
s/pattern/string/modifier: Find and Replace, default replaces only the first string in each line that is matched to the pattern
Sed ' s/oot/oot/'/etc/fatab
Sed ' s/^\//#/'/etc/fatab
Add modifier
G: Global Substitution
I: Ignore character case
s///: s###, [email protected]@@
Back reference: \ (\), \1, \2
L.. E:like-->liker
Love--lover
@: Reference pattern matches entire string
Sed ' s/\ (L.. e\)/\1r/g '/etc/fstab
Sed ' s/l. E/&r/g '/etc/fstab
Like--like
Love-->love
Sed ' s/l\ (.. e\)/l\1/g '/etc/fstab
Sed exercises:
1, delete the/etc/grub.conf file in the beginning of the blank character;
# sed-r ' ^[[:space:]]+ '/etc/grub.conf
2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;
# sed-r ' s/(ID:) [[:d igit:]]+ (: initdefault)/\15\2/g '/ETC/INITTB
3, delete the blank line in the/etc/inittab file;
# sed '/^$/d '/etc/inittab
4. Delete the # number at the beginning of the/etc/inittab file;
# sed ' s/^#//g '/etc/inittab
5, delete the beginning of a file # and the following white space characters, but requires the # number must be followed by a blank character;
# sed-r ' s/^#[[:space:]]+//g '/etc/inittab
6. Delete white space characters at the beginning of the line of the # class followed by white space characters in a file and #
# sed-r ' ^[[:space:]]+# '/etc/inittab
7. Take out a directory name for a file path;
File Directory Name:
# echo '/etc/rc.d/rc/rc.inittab ' | Sed-r ' s#^ (/.*/) [^/]+/?#\1#g '
:/ETC/RC.D/RC
Filename:
# echo '/etc/rc.d/rc/rc.inittab ' | Sed-r ' s#^/.*/([^/]+)/?#\1#g '
: Rc.inittab
linux--Text Processing sed