码迷,mamicode.com
首页 > 其他好文 > 详细

awk

时间:2016-08-23 17:29:52      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:使用   awk   

用法格式:awk [option] ‘script‘ FILE ...

awk [option] ‘/pattern/{action}‘ FILE ...

action: print $1

模式:

    地址定界: /pat1/ ,/pat2/

    /pattern/

    expression: 表达式

             >,>=,<,<=,==,!=,~

    BEGIN:   执行前做准备工作

    END:     执行后

awk的常用四种分隔符:

    输入:

             行分隔符

             字段分隔符

    输出:

              行分隔符

              字段分隔符

    awk的内置变量:

         NF: 字段数

         FS: 读入行时使用的字段分隔符

awk的

[root@192 ~]# awk ‘/^root\>/{print $1}‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@192 ~]# awk -F : ‘/^root\>/{print $1}‘ /etc/passwd
root

[root@192 ~]# awk -F : ‘/^root\>/{print $1,$7}‘ /etc/passwd
root /bin/bash

[root@192 ~]# awk -F : ‘/^root\>/{print "Username:", $1,$7}‘ /etc/passwd
Username: root /bin/bash

[root@192 ~]# awk -F : ‘/^root\>/{print "Username:", $1,"Shell:",$7}‘ /etc/passwd
Username: root Shell: /bin/bash

[root@192 ~]# df -h | awk ‘{print $2}‘
容量
18G
481M
490M
490M
490M
5.0G
497M
[root@192 ~]# df -h | awk ‘{print $2,$3}‘
容量 已用
18G 5.2G
481M 0
490M 92K
490M 7.0M
490M 0
5.0G 55M
497M 124M

[root@192 ~]# df -h | awk ‘/^\//{print $1}‘
/dev/mapper/centos-root00
/dev/sda3
/dev/sda1

[root@192 ~]# df -h
文件系统                   容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root00   18G  5.2G   13G   30% /
devtmpfs                   481M     0  481M    0% /dev
tmpfs                      490M   92K  490M    1% /dev/shm
tmpfs                      490M  7.0M  483M    2% /run
tmpfs                      490M     0  490M    0% /sys/fs/cgroup
/dev/sda3                  5.0G   55M  5.0G    2% /data
/dev/sda1                  497M  124M  373M   25% /boot


[root@192 ~]# df -h | awk ‘!/^文件系统/{print $1}‘
/dev/mapper/centos-root00
devtmpfs
tmpfs
tmpfs
tmpfs
/dev/sda3
/dev/sda1


[root@192 ~]# df -h | awk ‘/^文件系统/{print $1}‘
文件系统

[root@192 ~]# awk -F : ‘$3>=50000 {printf $1}‘ /etc/passwd
nfsnobody

[root@192 ~]# awk -F : ‘$7~/bash$/{print $1}‘ /etc/passwd
root
huaxia

[root@192 ~]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#

[root@192 ~]# awk ‘{print $NF}‘ /etc/inittab
systemd.
#
SYSTEM.
#
/usr/lib/systemd/system/ctrl-alt-del.target
#
targets:
#
3
5
#
run:
get-default
#
run:
TARGET.target
#

[root@192 ~]# awk -F : ‘BEGIN{print "UserName\n-------------"}$3>=50000{print $1}‘ /etc/passwd
UserName

-------------
nfsnobody

[root@192 ~]# awk -F : ‘BEGIN{print "UserName\n-------------"}$3>=50000{print $1}END{print "-------------\none users"}‘ /etc/passwd
UserName
-------------
nfsnobody
-------------
one users

[root@192 ~]# awk ‘BEGIN{FS=":"}$3>=500{print $1}‘ /etc/passwd
polkitd
colord
libstoragemgmt
saslauth
chrony
sssd
unbound
nfsnobody
gnome-initial-setup
huaxia


本文出自 “服务器运维” 博客,请务必保留此出处http://shamereedwine.blog.51cto.com/5476890/1841318

awk

标签:使用   awk   

原文地址:http://shamereedwine.blog.51cto.com/5476890/1841318

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!