• awk


    awk
    FS:输入字段分隔符
    OFS:输出字段分隔符
    RS:输入时的换行符
    ORS:输出的换行符
    NF:字段数量
    $NF:最后一个字段
    NR:统计行数
    FNR:各个文件的行数统计
    FILENAME:当前处理的文件名路径
    ARGC:命令行参数的个数
    ARGV:数组,以ARGV($1,...)调用
    1.[root@localhost ~]# awk -v FS=":" '{print $1}' /etc/passwd
    [root@localhost ~]# awk  '{print $1}' /etc/passwd
    [root@localhost ~]# gawk -v FS=":" '{print $1}' /etc/passwd相当于[root@localhost ~]# gawk -F: '{print $1}' /etc/passwd
    [root@localhost ~]# awk -v FS=":" '{print $1,$3,$7}' /etc/passwd
    [root@localhost ~]# awk -v FS=':' '{print $1,$3,$7}' /etc/passwd
    [root@localhost ~]# awk -v FS=':' -v OFS='#' '{print $1,$3,$7}' /etc/passwd
    [root@localhost ~]# awk -v RS=':' -v ORS='#' '{print}' /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@localhost ~]# awk '{print NF}' /etc/fstab 统计行字段个数;
    
    [root@localhost ~]# awk '{print $NF}' /etc/fstab cat /etc/fstab 显示最后一个字段
    
    [root@localhost ~]# awk 'BEGIN{print ARGC}' /etc/fstab /etc/issue
    3
    [root@localhost ~]# awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/issue
    awk
    [root@localhost ~]# awk -v test='glq hello' '{print test,$1}' /etc/fstab 
    glq hello 
    glq hello #
    glq hello #
    glq hello #
    [root@localhost ~]# awk 'BEGIN{test="welcome";print test,$2}' /etc/fstab 
    welcome 
    [root@localhost ~]# awk -v test='to my log' '{print test,$2}' /etc/fstab 
    to my log 
    to my log 
    to my log /etc/fstab
    to my log Created
    to my log 
    to my log Accessible
    [root@localhost ~]# awk -F: -v test="my log" '{print test,$7}' /etc/passwd
    my log /bin/bash
    my log /sbin/nologin
    my log /sbin/nologin
    
    ----------------------------
    
    printf
    [root@localhost ~]# awk -F: '{printf "username: %-15s,UID: %d
    ",$1,$3}' /etc/passwd
    username: root           ,UID: 0
    username: bin            ,UID: 1
    username: daemon         ,UID: 2
    username: adm            ,UID: 3
    [root@localhost ~]# awk -F: '{printf "username: %-10s,UID: %-1d,   Shell: %s
    ",$1,$3,$7}' /etc/passwd
    username: root      ,UID: 0,   Shell: /bin/bash
    username: bin       ,UID: 1,   Shell: /sbin/nologin
    username: daemon    ,UID: 2,   Shell: /sbin/nologin
    [root@localhost ~]# awk -F: '{printf "username: %-10s UID: %-1d   Shell: %s
    ",$1,$3,$7}' /etc/passwd
    username: root       UID: 0   Shell: /bin/bash
    username: bin        UID: 1   Shell: /sbin/nologin
    username: daemon     UID: 2   Shell: /sbin/nologin
    
    [root@localhost ~]# awk -F: '{$3>=1000?usertype="common user":usertype="system user";printf "%15s:%-s
    ",$1,usertype}' /etc/passwd
               root:system user
                bin:system user
             daemon:system user
                adm:system user
    ---------------------------------------
    [root@localhost ~]# awk '/^UUID/{print $1}' /etc/fstab 
    UUID=63539c8b-2d17-4c5e-aa11-e232515bba61
    [root@localhost ~]# awk '!/^UUID/{print $1}' /etc/fstab 
    
    #
    #
    #
    #
    #
    #
    #
    /dev/mapper/centos-root
    /dev/mapper/centos-swap
    
  • 相关阅读:
    数据产品设计专题(2)- 数据产品设计方法论之框架体系
    数据产品设计专题(1)- 数据产品设计方法论之互联网思维
    数据仓库专题(23):总线矩阵的另类应用-Drill Down into a More Detailed Bus Matrix
    数据仓库专题(22):总线架构和维度建模优势-杂项
    数据仓库专题(21):Kimball总线矩阵说明-官方版
    胖子哥的大数据之路(17):重构-基于商业本质看互联网思维
    胖子哥的大数据之路(16):数据采集标准-我们到底需要什么样的数据?
    android开发实践之1:安装部署环境设置
    MVC模式
    访问者模式
  • 原文地址:https://www.cnblogs.com/ligao/p/6385919.html
Copyright © 2020-2023  润新知