Regular expressions of shell scripts (c) --- awk tool

awk tool

In the Linux system, awk is a powerful tool for editing, the input text is read line by line, and to find the matching according to the specified pattern to meet the requirements of the output format or content filtering processing, may interact case without achieve fairly complex text manipulation, it is widely used in Shell script to complete a variety of automated configuration tasks.



awk common usage

Generally used awk command format is shown below, wherein the single quotation marks plus braces "{}" is provided for processing the data of the operation. awk can deal directly with the target file, the target file can be processed by the "-f" read the script.

awk 选项 '模式或条件 {编辑指令}' 文件 1 文件 2 „         //过滤并输出文件符条件的内容
awk -f 脚本文件 文件 1 文件 2 „        //从脚本中调用编辑指令,过滤并输出内容

awk inclined relative to a line into a plurality of "field" and then processed, and the default field separator is a space or tab keys. awk execution result can be displayed by the print data printing field function. In using awk command can use the logical operator "&&" indicates "and", "||" indicates "or" means "not", "!"; Also can perform simple mathematical operations, such as +, -, *, /,%, ^ respectively addition, subtraction, multiplication, division, exponentiation, and take the remainder.


In the Linux system / etc / passwd is a very typical file format, each field using the ":" as the delimiter separated, most of the log file is formatted Linux file system, extract the relevant information from these files It is part of the daily operation and maintenance of the work content. If you need to find out the username / etc / passwd, user ID, group ID, column, execute the following command to awk.

[root@192 ~]# awk -F ':' '{print $1,$3,$5}' /etc/passwd
root 0 root
bin 1 bin
daemon 2 daemon
adm 3 adm
lp 4 lp
sync 5 sync
shutdown 6 shutdown


awk operating principle

Regular expressions of shell scripts (c) --- awk tool
awk reads from the standard input file or input information, and as sed, the information is read line by line read. Awk except that the line of the text file as a record, and a part of the row (column) as a field (field) in a record. In order to operate these various fields, a method shell similar to the position of the variable awk borrowed by $ 1, $ 2, $ 3 "sequentially showing lines of different fields (records). Further awk by $ 0 represents the entire row (record). Different between the fields are separated by a specified .awk default character delimiter is a space to allow .awk command line with the "-F separator" specified in the form delimiters.



awk built-in variables (which can be directly used)

FS:指定每行文本的字段分隔符,默认为空格或制表位
NF:当前处理的行的字段个数
NR:当前处理的行的行号(序数)
$0:当前处理的行的整行内容
$n:当前处理行的第 n 个字段(第 n 列)
FILENAME:被处理的文件名
RS:数据记录分隔,默认为\n,即每行为一条记录


awk usage

1. Press the output text line

awk '{print}' try.txt            //输出所有内容,等同于 cat test.txt
awk '{print $0}' try.txt           //输出所有内容,等同于 cat test.txt
awk 'NR==1,NR==3{print}' try.txt           //输出第 1~3 行内容
awk '(NR>=1)&&(NR<=3){print}' try.txt           //输出第 1~3 行内容
awk 'NR==1||NR==3{print}' try.txt          //输出第 1 行、第 3 行内容
awk '(NR%2)==1{print}' try.txt           //输出所有奇数行的内容
awk '(NR%2)==0{print}' try.txt           //输出所有偶数行的内容
awk '/^root/{print}' /etc/passwd           //输出以root 开头的行
awk '/nologin$/{print}' /etc/passwd       //输出以 nologin 结尾的行
awk 'BEGIN {x=0} ; /\/bin\/bash$/{x++};END {print x}' /etc/passwd       //统计以/bin/bash 结尾的行数,等同于 grep -c "/bin/bash$" /etc/passwd
awk 'BEGIN{RS=""};END{print NR}' /etc/squid/squid.conf       //统计以空行分隔的文本段落数

Example demonstrates

[root@192 opt]#  awk '(NR%2)==1{print}' try.txt  //输出奇数行内容
1
3
5
7
9

[root@192 opt]# awk '(NR%2)==0{print}' try.txt  //输出偶数行内容
2
4
6
8
0

2. Output text field

awk '{print $3}' test.txt           //输出每行中(以空格或制表位分隔)的第 3 个字段
awk '{print $1,$3}' test.txt            //输出每行中的第 1、3 个字段
awk -F ":" '$2==""{print}' /etc/shadow         //输出密码为空的用户的shadow 记录
awk 'BEGIN {FS=":"}; $2==""{print}' /etc/shadow        //输出密码为空的用户的shadow 记录
awk -F ":" '$7~"/bash"{print $1}' /etc/passwd        //输出以冒号分隔且第 7 个字段中包含/bash 的行的第 1 个字段
awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services        //输出包含 8 个字段且第 1 个字段中包含 nfs 的行的第 1、2 个字段
awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}'/etc/passwd        //输出第 7 个字段既不为/bin/bash 也不为/sbin/nologin 的所有行

Example demonstrates

[root@192 opt]# awk -F ":" '$7~"/bash"{print $1}' /etc/passwd        //输出以冒号分隔且第 7 个字段中包含/bash 的行的第 1 个字段
root
czt
ccc
ccx
cax
max
etc

3. through the pipeline, double quotes invoke shell commands

1. Call the wc -l command bash statistics using the number of users, equivalent to grep -c "bash $" / etc / passwd

[root@testID opt]# awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
9

2. Call w command, and used to count the number of online users

[root@testID opt]# awk 'BEGIN {while ("w" | getline) n++ ;{print n-2}}'
5

3. Call the hostname, and the output current host name

[root@testID opt]# awk 'BEGIN {"hostname" | getline ; print $0}'
testID

Guess you like

Origin blog.51cto.com/14449521/2441389