惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - flyingchen

gcc的一个困惑 敏捷项目组 javascript string to date - flyingchen 熟悉了以下VIM指令,你会爱死她的 ajax应用如何做好seo - flyingchen - 博客园 nestful - ruby http-rest 客户端 分析 Liskov替换原则与继承 Linux Commands Cannot find autoconf. Please check your autoconf installation pecl报错(1) - flyingchen - 博客园 关于角色访问控制(RBAC) post xml 通过 simplexml_load_string 解析问题 - flyingchen php实现单件模式总结(持续更新) 激动网 PHP高级开发工程师 招聘 rsync的命令格式 rsync从linux到linux的文件同步备份 zend + apache + config stop words mmseg 安装错误 error: ’strncmp’ was not declared in this scope
awk学习
flyingchen · 2010-03-15 · via 博客园 - flyingchen

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]