态度决定高度、企图决定版图、格局决定结局

导航

awk学习

awk学习


基本语法:
awk -f {awk program file} filename
如果awk程序特别长,也可以将源文件分为多个文件,并执行:
awk -f source-file input-file1 input-file2 ...

预定义:
awk VariableMeaning
FILENAME文件名
RSInput record separator character (Default is new line)
OFSOutput field separator string (Blank is default)
ORSOutput record separator string (Default is new line)
NF每一条记录的Field数
NR记录顺序号(从1开始)
OFMT数字输出格式
FSField separator character (Blank & tab is default)
 
RS,FS运用:
如单条记录占多(三)行处理
Jane Doe
123 Main Street
Anywhere, SE 12345-6789

John Smith
456 Tree-lined Avenue
Smallville, MW 98765-4321


设置如下:
BEGIN{
   RS="";     #表示记录用空格分割。这里是空行
   FS="\n";  #表示Field用换行符分割
}

基础运算

$ cat > math
{
  print $1 " + " $2 " = " $1 + $2
  print $1 " - " $2 " = " $1 - $2
  print $1 " / " $2 " = " $1 / $2
  print $1 " x " $2 " = " $1 * $2
  print $1 " mod " $2 " = " $1 % $2
}


正则表达式
awk  '/正则表达式/ {print $0}' datafile
----用每个record匹配此正则,如果满足,则将此record赋值给$0

表达式匹配
exp ~  /regexp/ 返回true/false
exp !~ /regexp/ 返回true/false
可用于if( exp ~ /regexp/) do ...

如:if ($0 ~ /2400/ && $0 ~ /foo/) print

IGNORECASE = 1  :  正则匹配过程忽略大小写


初始化和运行结束

BEGIN {
                action 1
                action 2
                action N
                //设置Record分割符   RS=“/”  (!NOTE:如果RS="\0",那么整个数据文件将变成一个大字符串)
               //设置Field分割符   FS=“-”

             }
{
    //运行主体
               action1
               action2...
}
END{
               action1
               action2...
}

next
跳转到下一个record

格式化输出(和C一样)
%d:整数输出
%c:字符输出
%x:16进制输出
%s:字符串输出

格式补齐
       如:
        %3d,   %10s  : 前补齐
        %-3d , %-10s : 后补齐
       
调用系统指令
system(cmd)

getline输入
getline                              : 表示获取下一个record
getline var_name            : 表示获取下一个record,并赋值给var_name变量
getline var_name < "-"   : 表示从键盘输入,并赋值给var_name变量
getline < "filename"       : 表示从文件输入,并赋值给$0
"command" | getline       : 表示从command的执行结果输入,赋值给$0.
"command" | getline var_name  : 表示从command的执行结果输入,赋值给变量var_name

"|&"输入操作:
区别于普通的“|”方式,此方式是双向通信。如:
print "some query" |& "db_server"     #发送请求给dbserver
"db_server" |& getline  [var]                     # 从dbserver中获取返回结果,并赋值给$0或者var


Variant Effect
getlineSets $0, NF, FNR, and NR
getline var Sets var, FNR, and NR
getline < file Sets $0 and NF
getline var < file Sets var
command | getlineSets $0 and NF
command | getline var Sets var
command |& getlineSets $0 and NF. This is a gawk extension
command |& getline var Sets var. This is a gawk extension


从command-line赋值
如awk 'BEGIN{n=3} { print $n }' n=4 file1.data n=2 file2.data
从command-line赋值的参数具有“高优先级”,也就是虽然begin执行了,但n仍然等于
从command-line赋值的值。读取file1.data时为4,读取file2.data时为2。


字符串->数字
two = 2; three = 3
print (two three) + 4
---->结果是27。即(23)+4=27

Actions
[pattern] [{ action }]
[pattern] [{ action }]  --->读取Record,匹配pattern,匹配成功,执行action


Function
function name(parameter-list)
{
    body-of-function
}

Include
igawk指令支持在awk代码中包含@include指令,以包含需要的库。
如:
@include getopt.awk
@include join.awk

也可以:
设置AWKPATH环境变量,再通过awk/gawk启动脚本。


常用函数
length(string):计算长度
rand()             :生成随机数

SED

基本语法
sed -option 'general expression' [data-file]
sed -option sed-script-file [data-file]







posted on 2010-03-15 16:52  flyingchen  阅读(400)  评论(0编辑  收藏  举报