zoukankan      html  css  js  c++  java
  • awk

    1、AWK格式

    awk 'pattern' filename #例:awk '/root/' 1.txt
    awk {action}' filename #例:awk '{print $1}' 1.txt
    awk 'pattern{action}' filename #例:awk '/root/{print $1}' 1.txt

    2、AWK输出

    date|awk '{print"Month:"$2"
     Year:"$6}' #格式化输出
    awk '/root/' 1.txt #输出带有root的行
    awk '{print $1}' 1.txt #输出每一行的第一个字段
    awk '/root/{print "One:"$1}' 1.txt #输出包含root的行的第一个字段

    3、AWK自定义分隔符

    awk -F ':' '{print $1}' 1.txt #输出用:分割的第一个字段
    awk 'BEGIN{FS=":"}{print $1}' 1.txt #第二种指定分隔符的方法

    4、AWK匹配

    awk '$1~/[Rr]oot/' 1.txt #输出第一个字段包含Root或root的行
    awk '$3<5000' 1.txt #输出第三个字段小于5000的行

    5、AWK关系运算

    awk '$3==123' 1.txt #输出第三个字段等于123的行
    awk '$3>5000{print $1}' 1.txt #输出第三个字段大于5000的行的第一个字段
    awk 'sum=$1+$2{print sum}' 1.txt #输出每一行第一个字段加上第二个字段的和

    6、AWK内置变量

    FS 域的分隔符
    NF 每条记录的域的个数
    NR 已读的记录书

    7、AWK中BEGIN和END

    awk 'BEGIN{sum=0}{sum=sum+$5}END{pring sum}' 1.txt    #输出所有行第五个字段相加的和

    8、AWK输出重定向

    awk '{print $1>"2.txt"}' 1.txt    #把1.txt里面的第一个字段的值,写入到2.txt;里

    9、AWK中if的用法

    awk '{if($1>50)print $1"Too big";else print "Range is ok"}' 1.txt    #判断第一个字段是否大于50

    10、AWK循环

    awk '{i=1;while (i<NF){print $1;i++}}' 1.txt
    awk 'for(i=1;i<NF;i++) print $1' 1.txt

    11、AWK函数

    sub函数:字符串替换,只替换每条记录中出现的第一个字符串
    sub(正则表达式,替换串) #例:awk '{sub(/root/,"host");print}' 1.txt
    sub(正则表达式,替换串,目标串) #例:awk '{sub(/root/,"host",$5);print}' 1.txt
    
    gsub函数:字符串全局替换
    gsub(正则表达式,替换串) #例:awk '{gsub(/root/,"host");print}' 1.txt
    gsub(正则表达式,替换串,目标串) #例:awk '{gsub(/root/,"host",$5);print}' 1.txt
    
    length函数:返回字符串的长度
    length("字符串")  #例:awk '{print length(“hello”)}' 1.txt
    
    substr函数:取字符串 #例:awk '{print substr("hello motor",1,5)}' file

    12、脚本案例

    #!/bin/bash
    while read line
    do
      name='echo $line| awk -F'=' '{print $1}''
      value='echo $line|awk -F'=' '{print $2}''
      echo "name is $name,value is $value"
    done<config.bash
    #!/bin/bash
    while true
    do
      fcount=$(ls -l|grep log$|wc -l)
      if [ $fcount -gt 9 ]
      then
        dfile=$(ls -l|grep log$|awk 'NR == 1{print $9}')
        echo "delete file:$dfile"
        rm -rf $dfile
      else
        echo "file count<9"
      fi
    done
    #! /bin/sh
    #ll tomcat_zt
    pidlist='ps -ef |grep obs-new|grep -v "grep"|grep -v "00:00:00" |awk '{print $2}''
    echo "tomcat_obs list:$pidlist"
    #echo "$pidlist"
    'kill -9 $pidlist'
    cd /home/local/tomcat/apache-tomcat-obs-new/work
    'rm -rf Catalina'
    cd /home/local/tomcat/apache-tomcat-obs-new/bin
    sh startup.sh ;tail -f ../logs/catalina.out
  • 相关阅读:
    防删没什么意思啊,直接写废你~
    绝大多数情况下,没有解决不了的问题,只有因为平时缺少练习而惧怕问题的复杂度,畏惧的心理让我们选择避让,采取并不那么好的方案去解决问题
    Java 模拟面试题
    Crossthread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
    一步步从数据库备份恢复SharePoint Portal Server 2003
    【转】理解 JavaScript 闭包
    Just For Fun
    The database schema is too old to perform this operation in this SharePoint cluster. Please upgrade the database and...
    Hello World!
    使用filter筛选刚体碰撞
  • 原文地址:https://www.cnblogs.com/lilyo/p/11912666.html
Copyright © 2011-2022 走看看