Outline:
Color:
awk Basic Use
#####################################################
One, color: Shell, set the output text color (foreground color, background colors)
In the shell, you can set the color of the output font (foreground, background color)
Color |
Black |
Red |
Green |
Yellow |
Blue |
Purple |
Blue |
White |
Font encoding |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
Background code |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
1, the background color, font colors, font effects can be used alone can also be combined with the use of the order is not related, in English ";" separated
2, the background color, font colors, font effects have a m behind
3, before and after the string can have no space, if any, the output is also a space
Instance:
Echo-e "\033[30;47m Black character \033[0m"
Echo-e "\033[31m Red word \033[0m"
Echo-e "\033[32m Green word \033[0m"
Echo-e "\033[33m Yellow word \033[0m"
Echo-e "\033[34m Blue word \033[0m"
Echo-e "\033[35m Purple word \033[0m"
Echo-e "\033[36m Blue word \033[0m"
Echo-e "\033[37m White character \033[0m"
Ii. Basic use of awk
grep, sed, awk text processing tools, each with excellent
grep: Mainly used for text filtering, high efficiency relative to other tools
The SED flow Editor handles only the data in the pattern space by default, and operates in the behavior unit.
AWK reports the generator and displays the data after it is formatted. to be listed as units.
The origin of the awk command: named after its three authors
ALFRD Aho, Peter Weinberger, Brian Kernighan
You can match regular expressions , style loading , flow control , mathematical operators , Process Control statements , and even built-in variables and functions . It has almost all the beautiful features that a complete language should have. In the simplest sense,awk is a programming language tool for working with text.
Engineering Process:
Awk reads lines of text into memory, does not directly manipulate the text, after sharding (the default space symbol is a delimiter), the output, in the internal reference position variable, we can use positional variables to specify the fragment to be output.
Usage: awk [Options] ' {print '} ' file ...
#支持位置参数
Example
Output First Fragment
awk ' {print '} ' test.txt
Output print for awk
1. When you want to output multiple variables, the Print command uses {default, "comma"} to split, and when output, the default space is split
2. The output item can be a string or numeric value , a field of the current record (such as $ $), a variable , or an expression of awk; the value is first converted to a string and then output ;
3, the Print command after the item can be omitted, at this time its function is equivalent to print $, so if you want to output blank lines , you need to use print "";
Example: Output user login and login Shell ( you need to specify the input delimiter as: )
Awk-v fs=: ' {print $1,$7} '/etc/passwd
Awk-f: ' {print $1,$7} '/etc/passwd
awk Command Common variables:
FS # Field Delimiter
OFS # output Field delimiter
NF # current record , number of segments ($NF is the last field in the bank)
RS # Record delimiter, default is line feed
NR # Number of records processed by awk
FNR # The number of rows currently processed by the row in the current file
BEGIN
END
Example: Modifying an output delimiter
How field separators are specified
Awk-f: ' {print '} '/etc/passwd
#指定以 ":" As a delimiter
Awk-v fs=: ' {print $NF} '/etc/passwd
#指定以 ":" For delimiter-V to declare a variable
WK ' begin{fs= ":"}{print $1,$3} '/etc/passwd
#在命令执行之前为变量赋值
Awk-v ofs=: ' {print $1,$2} ' a.txt
#OFS specifying the Output field delimiter
User-defined variables
Gawk allows users to customize their own variables for use in program code, where variable name naming rules are the same as most programming languages, with only letters, numbers, and underscores, and cannot start with a number . The gawk variable name distinguishes the case of characters.
awk ' begin{var= ' xuegod test ';p rint var} '
#给变量赋值, and output the value of the variable
Awk-v var= "Xuegod test" ' begin{print var} '
#给变量赋值并输出变量的值
Actual combat: awk filters data (using scripting to calculate current memory usage)
[Email protected] ~]# vim mem.sh
#file---> mem.sh
#!/bin/bash
Limit=50
mem=$ (free-m | grep Mem | awk ' {print $} ')
total=$ (free-m | grep Mem | awk ' {print $} ')
tmp=$ (($Mem/$total))
echo "The used Memery is $tmp%."
If [$tmp-gt $limit]
Then
echo "Be carefull!"
Fi
[Email protected] ~]# sh mem.sh
1-27 awk Basic Use