12、shell_awk

AWK
awk is an excellent text processing tools, its name comes from the three names developer acronym.
   awk is not only a good document processing, you can program, write awk program
 
AWK basic format:
     awk [option] 'BEGIN{...}{...}END{...}' [file]
 
option:
  -F   specified field delimiter, if present in the file, then use FS =
  -f file   specified command file
 
 
BEGIN {...} opening paragraph, which is performed only once and the first command execution
 
{...} body, inside the command is executed multiple times, the number of document processing line,
      How many times is executed, before the equivalent loop (repeatedly)
 
END {...} End section, after running the preceding paragraphs, and then executed, the command which is executed only once
 
AWK use:
    1, the command line
awk -F: '{print $1}' /etc/passwd
 
    2, the command file
cat awk.cmd
  {
         print $5 "'s name is "$1","$5"'s userid is "$3
  }
 
awk -F: -f awk.cmd /etc/passwd
 
    3, with the interpreter awk
cat a1.awk
 
  #!/bin/awk -f
  {
         print $5 "'s name is "$1","$5"'s userid is "$3
  }
chmod 755 a1.awk
 
./a1.awk /etc/passwd
 
 
Execute multiple commands:
    Semicolon (;) separated
echo "My name is tom" | awk '{$4="mary";print $0}'
My name is mary
 
AWK can call the system command:
system (command name)
 
AWK variables
    AWK 有内置变量,也支持用户自定义变量
    内置变量:
$0 整行文本
$1 第一个字段
$2 第二个字段
$3 第三个字段
...
$NF 最后一个字段
NF 字段个数
NR 记录个数
 
    自定义变量
变量字母、数字、下划线组成,不能以数字开头,变量区分大小写
另:在awk 中,变量取值时,一般不要在前面加 $
 
如:
 
    自定义变量赋值的方法:
1、在脚本中(或命令行中)
   awk 'BEGIN{t="abc+defg";print t}'
 
 
2、在awk外部(如shell提示符)
  cat awk.cmd
    {
         print $n
 
    }
 
  awk -F: -f awk.cmd -v  n=7  /etc/passwd
 
 
AWK使用正则表达式
AWK使用正则表达式,正则表达式要出现在 // 中
 
如:打印包含root的行的用户和shell
AWK匹配操作符:  ~
格式:~ /正则表达式/
 
或:
     ! ~ /正则表达式/
 
    例:打印以n开头的用户
awk -F: '/^n/{print $0}' /etc/passwd
 
awk -F: '$0 ~ /^n/ {print $0}' /etc/passwd
 
    例:打印用户id以1开头的行
awk -F: '$3 ~ /^1/{print $1"   "$3"  "$7}' /etc/passwd
 
 
 
例:显示每个用户的用户名和它的shell
awk -F: '{print $1"             "$7}' /etc/passwd
 
例:显示每个磁盘的设备名、挂载点和它的使用百分比
df -h | awk '{printf("%-15s%-15s%-35s\n", $1,$6,$5)}'
 
 
另:awk中可以进行一些简单编程
    
    运算符:
数学运算符:
    >
    <
    >=
    <=
    ==
    !=
 
例:打印组id为0的用户
    awk -F: '$4=0{print $0}' /etc/passwd
    关系运算符:
    >
    <
    >=
    <=
    ==
    !=
    ~   
    !~
 
逻辑运算符
    ||
    &&
    !
 
AWK命令行中进行条件判断
条件放在{command}之前,不要if什么的(文件中要用if)
 
 
例:打印 用户id在10以内的用户名,id ,shell
 
 
AWK的流程控制:
    if (condition) {
 
commands
 
    }
 
    作用:判断condtion是否成立,成立执行commands
 
 
    if (condition) {
 
commands1
 
    }
    else
    {
 
commands2
 
    }
   作用:判断condtion是否成立,成立执行commands1,不成立执行commands2
 
 
    while ()
    {
commands
    }
  
 
 
 
    例:打印  /etc/passwd 偶数行
  awk 'NR%2 == 0{print $0}' aa.txt
 
    例:用awk 统计一个文件的空行数
#!/bin/awk -f
BEGIN{
        c=0
}
{
        if ($0 ~ /^ *$/) {
                c=c+1
        }
}
END {
        print c
}
 
[root@t183 test]# ./a2.awk aa.txt
5
    例:awk求指定数的阶乘,数由调时给定
[root@t183 test]# cat a3.awk
BEGIN {
         s=1
         while (n>1)   #n是由外部给定的求阶乘的数
         {
                 s=s*n
                 n=n-1
         }
         print s
}
 
[root@t183 test]# awk -f a3.awk -v n=5
     120
 
   练习:1、写一个日志分析程序,分析哪些ip访问过http服务器,
          什么时候访问的,访问的是什么页面
 
        2、修改主机名,把主机名固定到配置文件 /etc/sysconfig/network
           同时,将主机名和ip地址绑定到 /etc/hosts
 
   

Guess you like

Origin www.cnblogs.com/steven9898/p/11331207.html