Linux查找命令awk

http://man.lupaworld.com/content/manage/ringkee/awk.htm#id2874788   awk学习


awk 命令
awk '$1 + $2 < 100' test
awk '$1 > 5 && $2 < 10' test
awk '/^(no|so)/' test     -----打印所有以模式no或so开头的行。
awk '/^[ns]/{print $1}' test-----如果记录以n或s开头,就打印这个记录。
awk '$1 ~/[0-9][0-9]$/(print $1}' test-----如果第一个域以两个数字结束就打印这个记录。
awk '$1 == 100 || $2 < 50' test-----如果第一个或等于100或者第二个域小于50,则打印该行。
awk '/test/{print $1 + 10}' test-----如果记录包含正则表达式test,则第一个域加10并打印出来
awk '{print ($1 > 5 ? "ok "$1: "error"$1)}' test-----如果第一个域大于5则打印问号后面的表达式值,否则打印冒号后面的表达式值。
awk '/^root/,/^mysql/' test----打印以正则表达式root开头的记录到以正则表达式mysql开头的记录范围内的所有记录,如果找到一个新的正则表达式root开头的记录,
               则继续打印直到下一个以正则表达式mysql开头的记录为止,或到文件末尾
ariable = expression,如$ awk '$1 ~/test/{count = $2 + $3; print count}' test,上式的作用是,awk先扫描第一个域,一旦test匹配,
                  就把第二个域的值加上第三个域的值,并把结果赋值给变量count,最后打印出来。
awk 'BEGIN{FS=":"; OFS="\t"; ORS="\n\n"}{print $1,$2,$3} test。
awk 'END{print "The number of records is" NR}' test,上式将打印所有被处理的记录数。
awk '$1 = 100 {print $1 > "output_file" }' test。
awk 'BEGIN{ "date" | getline d; print d}' test。执行linux的date命令,并通过管道输出给getline,然后再把输出赋值给自定义变量d,并打印它。
awk 'BEGIN{"date" | getline d; split(d,mon); print mon[2]}' test。执行shell的date命令,并通过管道输出给getline,然后getline从管道中读取并将输入
      赋值给d,split函数把变量d转化成数组mon,然后打印数组mon的第二个元素。
awk 'BEGIN{while( "ls" | getline) print}'
awk 'BEGIN{printf "What is your name?"; getline name < "/dev/tty" } $1 ~name {print "Found" name on line ", NR "."} END{print "See you," name "."} test
awk 'BEGIN{while (getline < "/etc/passwd" > 0) lc++; print lc}'
awk '{print $1, $2 | "sort" }' test END {close("sort")}。awd把print语句的输出通过管道作为linux命令sort的输入,
                               END块执行关闭管道操作。

 awk '{if ($1 < $2) {count++; print "ok"}}' test
awk '{if ($1 > 100){ count++; print $1} else {count--; print $2}' test
 awk '{ i = 1; while ( i <= NF ) { print NF,$i; i++}}' test
awk '{for (i = 1; i<NF; i++) print NF,$i}' test

{for ( x=3; x<=NF; x++) 
            if ($x<0){print "Bottomed out!"; break}}
{for ( x=3; x<=NF; x++)
            if ($x==0){print "Get next item"; continue}}
      
awk '/^tom/{name[NR]=$1}; END{for(i in name){print name[i]}}' test
awk '{count[$1]++} END{for(name in count) print name,count[name]}' test
awk '{line[x++]=$1} END{for(x in line) delete(line[x])}' test
awk '{ sub(/test/, "mytest"); print }' testfile                  gsub 
awk '{ sub(/test/, "mytest"); $1}; print }' testfile
awk '{ print index("test", "mytest") }' testfile
awk '{ print length( "test" ) }' 
awk '{ print length }' testfile
substr( string, starting position )
substr( string, starting position, length of string )
awk '{ print substr( "hello world", 7,11 ) }' 
awk '{start=match("this is a test",/[a-z]+$/); print start}'
awk '{start=match("this is a test",/[a-z]+$/); print start, RSTART, RLENGTH }'
awk '{ print toupper("test"), tolower("TEST") }'
awk '{ split( "20:18:00", time, ":" ); print time[2] }'
awk '{ now = systime(); print now }'
awk '{printf("%s,",$1)}' filename

猜你喜欢

转载自zouhuiying.iteye.com/blog/2264731