on the Linux Three Musketeers simple induction:1: awk Exercise 1: Viewing server connection status and summarizing netstat in awk-an|awk'/^tcp/{++s[$NF]}end{for (a in s) print A,s[a]}'established1LISTEN -Exercise 2: Statistics of Web log access traffic, requests for output visits, page or picture requests, total size of each request, total access traffic size summary awk'{a[$7]+=$10;++b[$7];total+=$10}end{for (x in a) print b[x],x,a[x]| " Sort-rn ";p rint" Total size is: "All}'/app/log/access_logtotal Size is:172230 +/icons/poweredby.png83076 -/70546 8/icons/apache_pb.gif18608awk Basic usage: awk–f [: "] ' begin{event} {event}end{event} ' It can be used if, for, the syntax of Class C, such as arrays, in which the array subscript can be a string in addition to the number, which greatly enhances its textual statistical ability. In awk the variable can be used without first defining it, and the initial value is 0. About the use of commas: both awk and sed can be used as awk '/a/,/b/{event}end{event} ' filename matches all content from character A to first character B and later event handling Sed–n '/a/,/b/p ' filename matches all content from character A to the first character B and outputs to the screen2: sed common parameters:-I directly modify the contents of the document read, not output on the screen-N-election-sed can print the selected content when N and command p appear simultaneously-R Sed Using extended regular3: grep Common parameters:-I Ignore the difference in case of characters-W exactly finds the specified string, finds the line where the character is returned-O finds the specified character, returns only the contents of the search, typically adds an e mate, and finds the regular expression-The E option uses an extended regular expression exercise: count the number of keys in the device{} in the text below, and the key in other places does not count. If filename is Case.log:key isa linuxer.device: {ookey key Keyno SQL This isLogniu niu}keydevice: {key Keyno SQL This isLogniu Niu}key keydevice: {key Keyno SQL This isLogniu niu}device: {key Keyno SQL This isLogniu Niu} Key Personal Answer method:1: awk'/^device: {/,/}/{for (i=1;i<=nf;i++) if ($i ~/oldboy/) ++count;} End{print Count}' Case. Log explanation: Match all content that matches the beginning of the device and each match to the first}, then match the key in each field of each row, and finally output the NUM value of key. 2: Sed–n '/device: {/,/}/p ' Case. log | TR "" \ n "| Grep–w "Key" | WC-L Explanation: First use SED to match all the key lines in the device, use TR to replace the space with a newline, and then use grep to match the key line (each line a word string), and finally use Wc–l to count the number of rows and the NUM value of key. 3: Sed–n '/device: {/,/}/p ' Case. log | Awk–f "" ' { for(i=0; i<=nf;i++)if($i ~/key/) + +Count} The End{print count} ' sort command sorts the rows in the files specified by the file parameter and writes the results to standard output. If the file parameter specifies multiple files, the sort command connects the files and sorts them as a file. Common parameters:-N: Sort using "pure number" (by default it is sorted by text type);-r: Reverse sort;-T: delimiter, the default is to use the [tab] key to separate;-K: The meaning of sorting by that interval (field) sort is by default sorted by the first data, and by default is sorted in string form, by default from small to large order. Cat/etc/passwd | Sort-t':'-K3The third column, the small to the large uniq command, removes duplicate rows from the sorted file, so Uniq is often used in combination with sort. That is, in order for the Uniq to work, all duplicate rows must be contiguous. Common parameters:-I: ignore different uppercase and lowercase characters-C: to Count-u: Only show unique rows-d: Show only duplicate row cut commands to extract text columns from a text file or text stream. Common parameters:-D: followed by delimited characters. andF used together;-F: Divides a piece of information into segments based on the delimiter character of-D, using-F take out the meaning of paragraph; echo $PATH| Cut-d':'-F5Find the fifth path echo $PATH| Cut-d':'-F1-3,5find the first-to-third path of Echo $PATH| Cut-d':'-F3-find out how many lines, how many words, and how many characters are in the WC command statistics file in the third to last path. Common parameters:-L: only travel;-W: Only How many words (English word) are listed;-M: how many characters;
On the Linux Three Musketeers simple induction