大头壳

大头大头 下雨不愁 人家有伞 我有大头
posts - 1, comments - 6, trackbacks - 0, articles - 22
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

awk和shell的交互

Posted on 2008-03-28 10:03 王大头 阅读(692) 评论(0)  编辑 收藏 引用

awk -vA_VAR = $s_var 'BEGIN{print A_VAR;}'
也可以
awk 'BEGIN{print "'$s_var'";}'
 
在awk中执行shell命令,比如:
awk 'BEGIN{cmd="ls";system(cmd);}'
也可以用
awk 'BEGIN{cmd="ls"; print cmd | "sh";}'
还可以
awk 'BEGIN{cmd="ls"; print cmd;}' | sh
 
在awk中获取shell命令的结果,比如:
awk 'BEGIN{"date +%Y%m%d"|getline s_date;print s_date}'
 
      The built-in function getline sets $0 to the next input record from
      the current input file; getline < file sets $0 to the next record from
      file.  getline x sets variable x instead.  Finally, cmd | getline
      pipes the output of cmd into getline; each call of getline returns the
      next line of output from cmd.  In all cases, getline returns 1 for a
      successful input, 0 for end of file, and -1 for an error.
 
awk输出结果传给shell的命令,有两种方式。
awk 'BEGIN{for(i = 99; i > 0; i--) printf("%d\n", i) | "sort -n";}'
也开始使用
awk 'BEGIN{for(i = 99; i > 0; i--) printf("%d\n", i);}' | sort -n