First, SED (stream editor based on line)
1.1 Basic usage
SED does not edit the source file by default and only handles the data in the pattern space
Sed ' addresscommand ' file ...
Address representation Method:
1. Start and End lines
Example: 1,100 indicates the first row to 100 rows
2. Usage mode
The/regexp/uses the regular expression pattern. Example:/^root/a line that starts with root
3,/pattern1/,/pattern2/
Refers to the first time the line is matched by the pattern 1 to start, to the 1th time by the pattern 2 match to the end of the line, the middle of all the navigation
4, LineNumber
The specified row, $ represents the last line, $-1 represents the second
5, Startline,+n
Starting from Startline, n rows backward.
Command:
1) d: Delete qualifying rows
For example: sed "1,2d"/etc/fstab Delete the first two lines, after processing is finished, print the pattern space to the screen
Sed ' 3, $d '/etc/fstab
Sed '/^\//d '/etc/fstab displays lines starting with a slash
2) P: Display
Sed '/^\//p '/etc/fstab displays lines that begin with a slash, and the result matches the condition of the display twice
should use
Sed-n '/^\//p '/etc/fstab
3) a \ "string": Adds a row after the matched line, with the contents "string"
Sed-n '/^\//a \# Hello World '/etc/fstab
Add a row after the qualifying line Hello World
Sed-n '/^\//a \# Hello World\n#hello linux '/etc/fstab
Add a row after the qualifying line Hello World
4) I \string : Adds a row before the specified line, with the contents "string"
5) R and file name: Adds the contents of the specified file to the qualifying line
Example:
Sed ' 2r/etc/issue '/etc/fstab
Add the/etc/issue entire file after the second line.
Sed ' 1,2r/etc/issue '/etc/fstab
Add the/etc/issue entire file after line 1th and line 2.
6) W Save the content in the specified range to the specified file.
Sed-n '/oot/w/tmp/oot.txt '/etc/fstab
Store rows containing oot in the/etc/fstab to Oot.txt
7) s/pattern/string/modifier: Find and replace. The three slashes can be delimited with any character, such as [email protected]@@
Example
Sed ' s/oot/oot/'/etc/fstab find oot replaced with uppercase OT
Sed ' s/^\//#/'/etc/fstab replaces the slash at the beginning of the line with #
If you do not qualify the beginning of the line, only the string that is matched to the pattern for the first time in each row is replaced
Add modifier
g: Global substitution , just find replacement
Example sed ' s/\//#/g '/etc/fstab global replacement
I: Ignore case when finding
The grep latter reference is enclosed in front, followed by a \1, \2,\ number to refer to
Create file Test.txt, write content
Hello,like
Hi,my Love
Sed ' s#l. E#l. Er#g ' test.txt execution did not achieve the expected result because the latter does not support regular expressions
Sed ' s#l. E#&r#g ' Test.txt & can support latter reference
Hard-latter references use backslashes to escape parentheses
Sed ' s#\ (L.. e\) #\1r#g ' Test.txt
Using only the part of the previous match, you can only use latter to reference the
Change the like into like
To change love into love
Sed ' s#l\ (.. e\) #L \1#g ' Test.txt
Options
-N: Silent mode, does not display in mode space. It's only about the command.
-I : direct modification of source files with caution
-e script-e script: Each followed by a set of scripts, using multiple
-f/path/to/sed_script:
Sed-f/path/to/script File
-R: Allow extended regular expressions to be used
Example:
History | Sed ' s#^[[:space:]]*# #g '
===========================================
1. Remove whitespace characters from the beginning of the/etc/grub.conf file
2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5
3. Delete whitespace characters from the/etc/inittab file
4. Delete the # number at the beginning of the/etc/inittab file
4. Remove the # and the trailing white space characters from the beginning of the/etc/inittab file, #号后面有空白字符的才可以删
4. Delete white space characters and # (#前面的空白字符) in the line of a class with white space characters in a file.
5. Remove the directory name of a file path. For example:
/etc/rc.d
/var/log
echo "/ETC/RC.D" | Sed
Second, awk
2.1 Basic Usage
Sed and awk