awk笔记

来源:互联网 发布:java单元测试怎么做 编辑:程序博客网 时间:2024/05/01 08:38

awk -F":" '{ print "username: " $1 "/t/tuid:" $3 }' /etc/passwd

awk 'FS=":" {OFS="-"} {print NR "-->" $1, $2}' /etc/passwd

awk '{x=x+$3} {print NR, $3, x}' data
awk '{x=x+$3} END {print NR, $3, x}' data

awk '{x=x+$3} {print NR, $3, x | "sort -nr"}' data

FS="[[:space:]+]"

       NF          The number of fields in the current input record.
       NR          The total number of input records seen so far.
       OFMT        The output format for numbers, "%.6g", by default.
       OFS         The output field separator, a space by default.
       ORS         The output record separator, by default a newline.

awk中数组从1开始

for ( x in myarray ) {
    print myarray[x]
}
当 awk 在数组下标之间轮转时,它不会依照任何特定的顺序

delete fooarray[1]

print length(mystring)

print index(mystring,"you")

print tolower(mystring)
print toupper(mystring)
print mystring // unchanged

mysub=substr(mystring,startpos,maxlen)

print match(mystring,/you/), RSTART, RLENGTH
RSTART 包含返回值(第一个匹配的位置),RLENGTH 指定它占据的字符跨度(如果没有找到匹配,则返回 -1)。

sub(regexp,replstring,mystring)
sub() 将替换第一个 regexp 匹配(如果有的话),gsub() 将执行全局替换

numelements=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",mymonths,",")


test.awk
---------
BEGIN   { x=0 }
/^$/    { x=x+1 }
END     { print "I found " x " blank lines. :)" }
---------------
awk -f test.awk inputfile

摘录自:
http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-1/index.html
http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-2/index.html
http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-3/index.html

原创粉丝点击