Shell programming three swordsman awk
An introduction to awk tools
Awk is a programming language, mainly used to process text and data under linux/unix, and is a tool under linux/unix. Data can come from standard input, one or more files, or the output of other commands.
Awk's way of processing text and data: scan the file line by line, from the first line to the last line by default, look for lines matching a specific pattern, and perform the operations you want on these lines.
Awk represents the first letter of the author's last name. Because its author is three people, namely Alfred Aho, Brian Kernighan, Peter Weinberger.
Gawk is the GNU version of awk, which provides some extensions of Bell Labs and GNU.
The awk introduced below is based on GNU's gawk. In the Linux system, awk has been linked to gawk, so awk is all introduced below.
The role of awk tools
1. Awk is used to process files and data. It is a tool under Unix-like and a programming language
. 2. It can be used for statistics, such as website visits, IP visits, etc.
3. Support condition judgment, Support for and while loop
Three awk syntax format
awk option'command part' filename
note:
To quote shell variables, use double quotation marks to cause the
introduction of common options:
-F defines the field separation symbol, the default separator is a space
-v defines the variable and assigns a value
3.1 awk commonly used internal variables
variable | Variable description | Remarks |
---|---|---|
$0 | The entire line of the current processing line | |
$1,$2,$3…$n | Different fields in the file separated by interval symbols | awk -F: ‘{print $1,$3}’ |
NF | Number of fields in the current processing line | awk -F: ‘{print NF}’ |
$NF | last row | $(NF-1) means the penultimate column |
FNR / NR | The line number of the current processing line | |
FS | Define spacer | ‘BEGIN{FS=":"};{print $1,$3}’ |
OFS | Define the output field separator, the default space | ‘BEGIN{OFS="\t"};print $1,$3}’ |
RS | Enter record separator, default line break | ‘BEGIN{RS="\t"};{print $0}’ |
ORS | Output record separator, default line break | ‘BEGIN{ORS="\n\n"};{print $1,$3}’ |
3.2 Examples of commonly used internal variables
# awk -F: '{print $1,$(NF-1)}' 1.txt
# awk -F: '{print $1,$(NF-1),$NF,NF}' 1.txt
# awk '/root/{print $0}' 1.txt
# awk '/root/' 1.txt
# awk -F: '/root/{print $1,$NF}' 1.txt
root /bin/bash
# awk -F: '/root/{print $0}' 1.txt
root:x:0:0:root:/root:/bin/bash
# awk 'NR==1,NR==5' 1.txt
# awk 'NR==1,NR==5{print $0}' 1.txt
# awk 'NR==1,NR==5;/^root/{print $0}' 1.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Four advanced awk use
4.1 1. Formatted output print and printf
print函数 类似echo "hello world"
# date |awk '{print "Month: "$2 "\nYear: "$NF}'
# awk -F: '{print "username is: " $1 "\t uid is: "$3}' /etc/passwd
printf函数 类似echo -n
# awk -F: '{printf "%-15s %-10s %-15s\n", $1,$2,$3}' /etc/passwd
# awk -F: '{printf "|%15s| %10s| %15s|\n", $1,$2,$3}' /etc/passwd
# awk -F: '{printf "|%-15s| %-10s| %-15s|\n", $1,$2,$3}' /etc/passwd
awk 'BEGIN{FS=":"};{printf "%-15s %-15s %-15s\n",$1,$6,$NF}' a.txt
%s 字符类型 strings %-20s
%d 数值类型
占15字符
- 表示左对齐,默认是右对齐
printf默认不会在行尾自动换行,加\n
4.2 awk variable definition
# awk -v NUM=3 -F: '{ print $NUM }' /etc/passwd
# awk -v NUM=3 -F: '{ print NUM }' /etc/passwd
# awk -v num=1 'BEGIN{print num}'
1
# awk -v num=1 'BEGIN{print $num}'
注意:
awk中调用定义的变量不需要加$