One
Sed:stream Editor, stream editors;
awk (gawk): Text formatting tool, Report Builder
Two SED stream editor for filtering and transforming text
sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...
Working mechanism: Each time a line of text is read into "pattern space", the processing is done in the mode space, and the processing result is output to the standard output device;
Command parameters
-R supports extended regular expressions;
-n silent mode;
-E script1-e script2-e SCRIPT3 specifies multiple scripts to run;
-f/path/to/script_file reads the script from the specified file and runs it;
-I directly modify the source file;
Basic regular Expression meta-characters:
Character matching:., [], [^]
Number of matches: *, \?, \+, \{m,n\}, \{n\}
Position anchoring: ^, $, \<, \>
Grouping and referencing: \ (\), \1, \2, ...
Multiple Choice one: a|b|c
Address delimitation:
# Specify rows
$ last line
/regexp/any row that can be matched to by regexp
\%regexp% Ibid, except for the regexp boundary character
/regexp/| Ignore character casing when matching
\%regexp%| Ignores character case when matching, except for% as RegExp boundary character
Startline,endline:
#,/regexp/starts from the # line, to the end of the line to which the first/regexp/is matched, and all the rows in the middle;
#,#
/regexp1/,/regexp2/starts from the first line that is matched by the/regexp1/, to the end of the line to which the first match is/regexp2/, and all the rows in the middle;
#,+n starts from the # line and continues to the n rows down;
FIRST~STEP Specifies the starting line, and the step size; 1~2,2~2
Edit commands for SED
D Delete rows in the pattern space
= line number is displayed
A \text additional text
I \text insert text, support \ n implement multi-line insertion
C \text replace the matching line with text
P lines in print mode space
S/regexp/replacement/replaces the content that is matched by regexp to replacement
G Global Substitution
W/path/to/somefile Save the specified content to the file specified by the/path/to/somefile path
R/path/from/somefile inserts all the contents of another file at the specified location in the file, completing the file merge
[Email protected] ~]# echo Sunday | Sed ' s/day/night/' #替换Sunday中的day为night
Sunnight
Advanced command:
H uses the content of the pattern space to cover the contents of the preserving space;
H Append the contents of the pattern space to the back of the content in the hold space;
G takes its content from the hold space and overwrites it with the contents of the pattern space;
G takes its content from the hold space and appends it to the content in the pattern space;
X to keep the space and pattern space in exchange;
N reads the next line of the matched row to the pattern space (which overwrites the original content in the pattern space);
N reads the next line of the matched row to the pattern space, appended to the original content in the pattern space;
D Delete the contents of the mode space;
D Delete the first row in the multiline mode space;
Note: Command function can be used! Reverse; semicolons can be used to separate scripts;
Example:
Sed ' G '/etc/issue: Adds a blank line behind each line in the file;
Sed ' $!d '/etc/fstab: Keep the last line;
Sed '/^$/d; G '/etc/issue: Ensure that the specified file has only one blank line behind each line;
Sed ' n;d '/etc/issue: reserved odd lines;
Sed-n ' 1! g;h; $p '/etc/issue reverse output a file
Sed ' $! n;$! D '/etc/issue
Three awk
This article is from the "Progress a little every day" blog, be sure to keep this source http://1step.blog.51cto.com/1160799/1697101
Sed & awk