grep, sed and awk are quite useful!
Gerp Find, sed edit, awk analyzes and processes it based on content.
awk (keyword: analysis & processing)Parsing of one line of processing awk ' condition type 1{action 1} condition type 2{action 2} ' filename, awk can also read standard input from the previous instruction
Unlike SED, which is often used for a whole line of processing, awk prefers to divide a row into several "fields" (areas), with the default delimiter being the SPACEBAR or TAB key
For example:
Last-n 5 | awk ' {print ' \ t ' $ ' $} ' there is no space between "\ t" in curly braces here, but it is best to add a space, and note that "\ T" is double-quoted, because the contents are in single quotes
$ A represents the entire row, representing the first region, and so on
The processing process for awk is:
1. Read the first line and fill in the first line with the variable ... In equal variables
2. Perform the action according to the condition limit
3. Next line of execution
As a result, awk is processed one line at a time, and the smallest unit processed at a time is a region
There are also 3 variables, NF: The number of fields processed per row, NR currently processed to the first few lines of FS current delimiter
Logic judgment > < >= <= = =!==, assignment direct use =
cat/etc/passwd | awk ' {fs= ': '} $3<10 {print ' \ t ' $ ' $} ' first defines the delimiter as:, then judge, watch, judge not write in {}, then perform the action, fs= ":" This is an action, assignment action, not a judgment, so do not write in {}
Begin END, which gives the programmer an initialization and finishing work, the actions listed after begin will be executed within {} Before awk begins scanning the input, and the actions within end{} will be executed after the input file has been scanned.
awk '/test/{print NR} ' ABC prints the line number of the line with test, note that regular expressions can be used between//
Within awk {}, you can use if else, for (i=0;i<10;i++), i=1 while (I<NF)
VisibleMany of Awk's uses are equivalent to the C language, such as "\ T" delimiter, print format, if, while, and so on
Awk is a fairly complex tool, and when it's really used, add it. (Picture of the tool)
sed (keyword: edit)Text editing tools with behavioral units sed can directly modify the file, but it is generally not recommended to do so, you can analyze the standard input
Basic working methods: sed [-NEF] ' [action] ' [input text]
-N: Quiet mode, in general SED usage, data from stdin is generally listed on the screen, and if the-n parameter is used, only the line that is processed by SED is listed.
-E: Multiple edits, such as you want to delete a row at the same time, and want to change the other rows, then you can use Sed-e ' 1,5d '-e ' s/abc/xxx/g ' filename
-F: First write the action of SED in a file, and then through the Sed-f ScriptFile can directly perform the SED action within the ScriptFile (no experimental success, not recommended)
-I: Direct editing, this time is really changing the contents of the file, and everything else just change the display. (Not recommended)
Action:
A is added, a string is followed by a, and the string appears on a new line. (Next line)
C supersedes, C-strings, which can replace rows between n1,n2
D Delete, not after anything.
I insert, followed by a string that appears on the previous line
P prints, lists selected data, usually works with Sed-n sed-n ' 3p ' only prints line 3
s substitution, similar to the substitution in VI, 1,20s/old/new/g
[Line-address]q exit, match to a line to exit, improve efficiency
[Line-address]r matches to a line to read a file for example: sed ' 1r qqq ' ABC, note that the written text is written on the 1th line behind, that is, line 2nd
[Line-address]w file, the line to which the match is written to a file such as: Sed-n '/m/w QQQ ' ABC, reads the line with M from ABC to the QQQ file, note that this write has coverage.
example:
sed ' 1d ' ABC delete the first line in the ABC file, note that All rows except the first line are displayed, because the first row has been deleted (the actual file has not been deleted, but it was deleted only when it was displayed)
sed-n ' 1d ' ABC does not display any content, Because the line processed by SED is a delete operation, it is not realistic.
sed ' 2, $d ' ABC removes all the contents from the second line to the last line in ABC, note that the $ sign in the regular expression represents the end of the line, but this does not say at the end of that line, it will refer to the end of the last line, ^ Start, if you do not specify which line begins, then the first line starts
sed ' $d ' ABC only deletes the last line, because it is not specified as the end of the line, it is considered to be the end of the last line
sed '/test/d ' abc file are removed
sed '/test/a rrrrrrr ' abc adds rrrrrrr to all TES The next line of the T line is also possible through the line sed ' 1,5c rrrrrrr ' abc
sed '/test/c rrrrrrr ' ABC will rrrrrrr replace all with test Yes, of course, it can also be replaced by rows, such as sed ' 1,5c rrrrrrr ' abc
grep (keyword: intercept)Text-gathering tool, combined with regular expression is very powerful
Main parameters []
-C: Output only matching rows
-I: Case insensitive
-H: Do not display file names when querying multiple files
-L: Only file names with matching characters are output when querying multiple files
-N: Displays matching line numbers and rows
-V: Displays all lines that do not contain matching text (I often use the remove grep itself)
Basic how to work: grep to match the content file name, for example:
grep ' test ' d* shows all rows with test in the file beginning with D
grep ' Test ' AA bb cc shows lines that contain test in the AA bb cc file
grep ' [a-z]\{5}\ ' AA displays all strings containing a string of at least 5 consecutive lowercase letters
Grep,sed,awk