• 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
    
  • 相关阅读:
    HDU2159 二维完全背包
    HDU1401 BFS
    HDU2842 矩阵乘法
    CF2.E
    CF2.D
    *HDU2254 矩阵乘法
    CF2.C
    *HDU1907 博弈
    博弈
    *HDU2147 博弈
  • 原文地址:https://www.cnblogs.com/ligao/p/6385919.html
Copyright © 2020-2023  润新知