One. awk
1. What is awk
Awk is a programming language. It has the common function of the general programming language.
Because the awk language has certain characteristics, such as: using the literal translation device (interpreter) does not need to compile; Variable no type (typeless), you can use the text when the array subscript (associative array) ... and other features. Therefore, using awk to compose a program is simpler and more convenient and time-saving than using other languages. Awk also has some built-in functionality that makes awk adept at processing data with a row (Record), field type; In addition, AWK has a pipe function that can transfer processed data to external shell commands for processing, and then transfer the processed data from the shell command back to the AWK program, which makes it easy for the AWK program to use system resources.
2.awk syntax
Synopsis
awk [Options] ' program ' File File ...
awk [Options] ' pattern{action} ' file File ...
Note: The Linux version of awk is a soft link linked to gawk
Pattern: Usually use "relational expressions" (relational expression) to act as
Actions: Consists of many awk directives that comprise: print, printf
3.awk built-in variables
Variable name |
Variable description |
$ |
Whole line |
$ |
First Field |
$ |
A second field |
NF |
Number of fields |
Nr |
Number of rows, all files are counted in conjunction |
FNR |
Number of rows, each file counted |
Argv |
Array, save the character of the command itself |
ARGC |
Save the number of arguments in the awk command |
FILENAME |
The name of the current file being processed |
Custom variables:
-V Var_name=value
Variable names are case-sensitive;
(1) Variables can be defined in program;
(2) You can customize variables by using the-v option in the command line;
4.printf
Use format for commands: printf format, item1, item2,...
(1) to specify format;
(2) will not be automatically wrapped, if you want to change the line, you need to give \ n
(3) format is used to specify its output format for each subsequent item;
The format indicator is preceded by%, followed by a character:
%c: The ASCII code that displays the characters;
%d,%i: decimal integer;
%e,%e: The scientific counting method shows the numerical value;
%f: Displays floating-point numbers;
%g,%g: Displays values in scientific notation format or floating-point number format;
%s: Display string;
%u: Displays unsigned integers;
Percent: show% itself;
Modifier:
#: Display width
-: Align Left
+: Display symbols for numeric values
. #: Value Accuracy
Cases
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/73/9A/wKioL1YCOMWTA4UcAAAWuil9Q-c603.jpg "height="/>
5.awk output Redirection
Writes the results of the awk output directly to the file
Print Items > Output-file
Print Items >> output-file
Cases
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/73/9A/wKioL1YCOMWR5NhIAAAxqQOSOag320.jpg "height="/>
6. Mode
(1) Regexp: Format is/pattern/
Only the rows that are matched to the/pattern/are processed;
(2) Expression: expressions that satisfy a condition when the result is a non-0 or a non-empty string;
Only the rows that satisfy the condition are processed;
(3) Ranges: Line range, previous address delimitation,
Nr
Processing only rows within a range
(4) Begin/end: Special mode, executed only once before the program of the awk command is run (BEGIN) or after run (END);
(5) Empty: null mode, matching any row;
Cases
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/73/9A/wKioL1YCOMaxk888AAAg7CHYchE014.jpg "height="/>
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/73/9D/wKiom1YCOMSSAv36AAAZ9eEJOpo432.jpg "height="/>
7.
Action
(1) Expressions
(2) Control statements
(3) Compound statements
(4) Input statements
(5) Output statements
8. Control statements
If-else
Format: if (condition) {then body} else {else body}
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/73/9D/wKiom1YCOMWyOsdsAAASEfdPtAU486.jpg "height="/>
While
Format: while (condition) {while body}
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/73/9D/wKiom1YCOMWTmlbtAAATcAEDyKo037.jpg "height="/>
Note: It is easy to create a dead loop without setting an end condition
Do-while Cycle
Format: do {Do-while body} while (condition)
For loop
Format: for (variable assignment; condition, iteration process) {for body}
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/73/9A/wKioL1YCOMbSNmYnAAAS-1rrdCk860.jpg "height="/>
Case statement
Syntax: switch (expression) {case VALUE or/rgeexp/: statement1; ... default:stementn}
Two. sed
1.sed Introduction
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.
2.sed syntax
sed [options] ' command ' file (s)
sed [options]-F scriptfile file (s)
3. Meta Character set
^: The beginning of the anchoring line is as follows:/^sed/matches all lines beginning with sed.
$: The end of the anchor line, such as:/sed$/matches all lines ending in sed.
.: Matches a non-newline character such as:/s.d/matches the s followed by an arbitrary character followed by D.
*: Match 0 or more words such as:/*sed/match all the templates are one or more spaces followed by the SED line.
[]: matches a specified range of characters, such as/[ss]ed/matching sed and sed.
[^]: matches a character that is not within the specified range, such as:/[^a-rt-z]ed/matches a letter that does not contain a-r and t-z, immediately following the line of Ed.
\(.. \): Save matching characters, such as s/\ (love\) able/\1rs,loveable is replaced with lovers.
&: Save search characters to replace other characters, such as S/love/**&**/,love, which is **love**.
\<: Anchors the beginning of a word, such as:/\<love/matches a line containing a word that begins with love.
\>: Anchors the end of a word, such as/love\>/, to match lines that contain words ending with love.
X\{m\}: Repeats characters x,m times, such as:/0\{5\}/matches rows that contain 5 O.
X\{m,\}: Repeat the character X, at least m times, such as:/o\{5,\}/matches at least 5 rows of O.
X\{m,n\}: Repeat the character X, at least m times, not more than n times, such as:/o\{5,10\}/matches rows of 5--10 O.
Find and replace text in 4.vim editing:
Address delimitation s/What to look for/replace with content/
What to look for: You can use regular expressions
Replace with: Regular expressions are not supported, but support references to the contents of the preceding regular expression groupings
Address delimitation:%, Startline,endline
5.sed working mechanism
Each time a line of text is read into "pattern space", processing is done in the pattern space, and the processing result is output to the standard output device;
-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;
6.sed Editing commands
D Delete
s replace% for all files
I insert, edit files directly
A plus
7. Practice
(1) Remove whitespace characters from the beginning of all lines in the/boot/grub/grub.conf file;
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/73/9D/wKiom1YCOMXQ84yqAABwKbJOOwE809.jpg "height=" 129 "/>
(2) Remove all # and white space characters from the beginning of the line at the beginning of #, followed by at least one white-space character, in the/etc/fstab file;
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/73/9D/wKiom1YCOMWBJGUQAABjeW-pTNI619.jpg "height="/>
(3) Save the odd line of the/etc/fstab file as a/tmp/fstab.3;
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/73/9D/wKiom1YCOMajYsnnAABloVsv4Wg148.jpg "height=" 123 "/>
(4) Echo a file path to the SED command, take out its base name, and further, take out its path name;
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/73/9A/wKioL1YCOMeRbVPcAAAU-aAgwOw969.jpg "height="/>
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/73/9D/wKiom1YCOMagGdhLAAATG2NVEDw151.jpg "height="/>
With Margo Linux (Lesson 8) awk & sed Function summary