1. built-in variables
variable name | description |
---|---|
FS | Input field separator, the default is a space or tab |
OFS | Output field separator, the default is a space |
RS | Input record separator, default newline \ n |
ORS | Output record separator, default newline \ n |
NF | Count the number of fields in the current record |
NO | No statistical records, each dealing with his record, number will be +1 |
FNR | No statistical records, each dealing with his record, number will be +1, and NR difference is that, when dealing with the second file, numbered re-count. |
ARGC | The number of command line parameters |
ARGV | Command line parameter array array sequences, the start index from 0, ARGV [0] is awk |
ARGIND | The current file being processed index value. The first file is 1, 2 is the second file, and so on |
ABOUT | The current system environment variables |
FILENAME | Output current file name processing |
IGNORECASE | Ignore case |
SUBSEP | Array subscript separator, default is "\ 034" |
1.1. FS and OFS
Before the program starts to copy the FS, as with the -F
[root@192 ~]# head -n5 /etc/passwd
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
[root@192 ~]#
[root@192 ~]# head -n5 /etc/passwd|awk 'BEGIN{FS=":"}{print $1,$2}'
root x
bin x
daemon x
adm x
lp x
OFS由于默认是空格,输出如果是:,那么逗号会变为*
[root@192 ~]# head -n5 /etc/passwd|awk 'BEGIN{FS=":";OFS="*"}{print $1,$2}'
root*x
bin*x
daemon*x
adm*x
lp*x
1.2. RS and ORS
RS默认\n,指定分割符 ,ORS默认换行符,也可以指定输出符号
也就是说 将分割符换成你想要的样子
[root@192 ~]# tail -n2 /etc/services |awk 'BEGIN{RS="/";ORS="+"}{print $0}'
iqobject 48619+udp # iqobject
matahari 49000+tcp # Matahari Broker
1.3. NF
字段统计
[root@192 ~]# tail -n2 /etc/services |awk 'BEGIN{FS="/"}{print $NF}'
tcp # 3GPP Cell Broadcast Service Protocol
tcp # Image Systems Network Services
[root@192 ~]# tail -n 2 /etc/services |awk 'BEGIN{FS="/"}{print NF}'
2
2