linux awk - awk tutorial

【基本介绍】
awk是一款强大的对文件内容进行处理的软件,可以当作是一个脚本语言。
这里列出大概的学习框架和简单例子。


【学习列表】
1.Built-in functions for numeric operations
2.Built-in functions for String operations
3.Build-in functions for input output operations
4.awk if statement(if,if else,if else if,:?)
5.awk loops(do while,for loop,break,continue,exit)
6.Build-in variables(FS,OFS,RS,ORS,NR,NF,FILENAME,FNR)


【简单例子】
1. number operations
int log sqrt exp sin cos atan2 rand srand ...

$cat rand.awk
BEGIN {
while(i<1000)
{
n = int(rand()*100);
rnd[n]++;
i++;
}
for(i=0;i<=100;i++) {
print i,"Occured", rnd[i], "times";
}
}
$


2. string operations
index length match split sprintf sub gsub gensub substr tolower toupper
[code="linux"]
# awk 'BEGIN{str="daabaaa" ;sub(/a/,"c&c",str) ;print str}'
dcacabaaa
[/code]

3. input output operations
close fflush system
[code="linux"]
# awk 'BEGIN{print date}'
# awk 'BEGIN{system("date")}'
Tue Oct 21 10:40:16 CST 2014
[/code]


4. if statement

$ awk '{
if ($3 >=35 && $4 >= 35 && $5 >= 35)
print $0,"=>","Pass";
else
print $0,"=>","Fail";
}' student-marks
Jones 2143 78 84 77 => Pass
Gondrol 2321 56 58 45 => Pass
RinRao 2122 38 37 => Fail
Edwin 2537 87 97 95 => Pass
Dayan 2415 30 47 => Fail


5. for loop ,break continue

$ awk 'BEGIN{
count=1;
do
print "This gets printed at least once";
while(count!=1)
}'
This gets printed at least once


6. build-in variables
FS - Input field separator variable.
OFS - Output Field Separator Variable
RS - Record Separator variable
ORS - Output Record Separator Variable
NR - Number of Records Variable
NF - Number of Fields in a record
FILENAME - Name of the current input file
FNR - Number of Records relative to the current input file

$ awk -F':' 'BEGIN{OFS="=";} {print $3,$4;}' /etc/passwd
41=41
100=101
101=102
103=7
105=111
110=116
111=117
112=119


【参考链接】
[url]http://www.thegeekstuff.com/tag/awk-tutorial-examples/[/url]
[url]http://www.math.utah.edu/docs/info/gawk_13.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Awk入门教程。作者 Bruce Barnett 注:英文版。以下是目录 Why learn AWK? Basic Structure Executing an AWK script Which shell to use with AWK? Dynamic Variables The Essential Syntax of AWK Arithmetic Expressions Unary arithmetic operators The Autoincrement and Autodecrement Operators Assignment Operators Conditional expressions Regular Expressions And/Or/Not Commands AWK Built-in Variables FS - The Input Field Separator Variable OFS - The Output Field Separator Variable NF - The Number of Fields Variable NR - The Number of Records Variable RS - The Record Separator Variable ORS - The Output Record Separator Variable FILENAME - The Current Filename Variable Associative Arrays Multi-dimensional Arrays Example of using AWK's Associative Arrays Output of the script Picture Perfect PRINTF Output PRINTF - formatting output Escape Sequences Format Specifiers Width - specifying minimum field size Left Justification The Field Precision Value Explicit File output AWK Numerical Functions Trigonometric Functions Exponents, logs and square roots Truncating Integers "Random Numbers The Lotto script String Functions The Length function The Index Function The Substr function GAWK's Tolower and Toupper function The Split function NAWK's string functions The Match function The System function The Getline function The systime function The Strftime function User Defined Functions AWK patterns Formatting AWK programs Environment Variables ARGC - Number or arguments (NAWK/GAWK) ARGV - Array of arguments (NAWK/GAWK) ARGIND - Argument Index (GAWK only) FNR (NAWK/GAWK) OFMT (NAWK/GAWK) RSTART, RLENGTH and match (NAWK/GAWK) SUBSEP - Multi-dimensional array separator (NAWK/GAWK) ENVIRON - environment variables (GAWK only) IGNORECASE (GAWK only) CONVFMT - conversion format (GAWK only) ERRNO - system errors (GAWK only) FIELDWIDTHS - fixed width fields (GAWK only) AWK, NAWK, GAWK, or PERL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值