`

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
[root@pandaVM script]# awk 'BEGIN{str="daabaaa" ;sub(/a/,"c&c",str) ;print str}'
dcacabaaa


3. input output operations
close fflush system
[root@pandaVM script]# awk 'BEGIN{print date}'
[root@pandaVM script]# awk 'BEGIN{system("date")}'
Tue Oct 21 10:40:16 CST 2014



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


【参考链接】
http://www.thegeekstuff.com/tag/awk-tutorial-examples/
http://www.math.utah.edu/docs/info/gawk_13.html
分享到:
评论

相关推荐

    Awk - A Tutorial and Introduction - by Bruce Barnett.pdf

    Awk is an extremely versatile programming language for working on files. We'll teach you just enough to understand the examples in this page, plus a smidgen. The examples given below have the ...

    awk tutorial and introduction 英文版

    **awk教程与简介** awk是一种强大的文本分析工具,广泛用于处理和解析结构化的...参考链接提供的在线教程(&lt;http://www.askapache.com/linux/awk-tutorial.html&gt;)会有更详细的实例和解释,帮助你进一步提升awk技能。

    sed-awk-2nd-edition.chm

    The book begins with an overview and a tutorial that demonstrate a progression in functionality from grep to sed to awk. sed and awk share a similar command-line syntax, accepting user instructions in...

    Awk入门教程 《Awk A Tutorial and Introduction - by Bruce Barnett》

    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 ...

    AWK Tutorial Guide

    AWK是一种强大的文本分析工具,尤其在Linux和Unix环境中广泛使用。它被设计用来处理结构化的数据,如CSV、TSV文件或者任何包含固定格式的数据流。AWK的名字来源于它的三位创始人——Alfred Aho、Peter Weinberger和...

    Bulgarian Linux Tutorial-开源

    7. **terminal.html** - 终端命令行:可能详细介绍了Linux命令行的使用,包括常用的shell命令,如grep、sed、awk等。 8. **tutor.html** - 实战指导:可能是对前面知识的应用实例,如配置网络服务,安装软件,进行...

    收集与整理linux日常使用中的命令工具等_Linux-Tutorial.zip

    `awk`是一个强大的文本分析工具,可以进行复杂的文本处理和数据提取。 对于系统监控,`top`和`htop`命令可以实时显示系统中进程的动态视图,并提供资源使用统计。`df`和`du`命令则分别用于查看磁盘空间使用情况和...

    awk使用详解(官方文档翻译)

    ### AWK 使用详解知识点 #### 一、AWK 概述与适用场景 - **AWK 的定位**:AWK 是一种专为文本处理设计的脚本语言,它结合了强大的文本处理能力和易于上手的特点,非常适合用于数据分析、报告生成等应用场景。 - **...

    Linux_命令行_Bash_的基本用法和脚本编程_-_阮一峰__bash-tutorial.zip

    除此之外,教程还会深入到更高级的编程技巧,如函数的定义和使用、数组的操作、正则表达式的应用、文本处理工具(如grep、sed、awk等)的使用,以及如何调试脚本。这些内容将帮助读者编写出更加复杂和实用的脚本,...

    linux shell programming tutorial

    根据提供的文件信息,我们可以归纳出一系列关于 Linux Shell 编程的关键知识点。以下是对这些知识点的详细解释: ### Linux Shell 编程教程 #### 引言 本教程旨在为初学者介绍基本的 Shell 编程概念,并通过示例来...

    HackerRank-The-Linux-Shell-Challenges-Solutions:Linux Shell的完整解决方案和相关教程-Bash,文本处理,Bash中的数组,Grep Sed Awk在HackerRank上的挑战

    描述通过针对HackerRank上Linux Shell挑战的相关学习资源,汇编了所有容易,中等和困难挑战的解决方案。 域包括:重击挑战数字挑战性解决方案1个2个3解决方案4 用数字循环解决方案5 数字世界解决方案6 比较数字解决...

    Linux rute tutorial

    - **文本处理**:学习grep、awk等工具来处理文本文件。 - **进程管理**:熟悉ps、kill等命令来查看和控制运行中的进程。 - **网络工具**:介绍ping、curl等用于网络测试和数据传输的工具。 #### 五、正则表达式 ...

    Unix-tutorial

    教程中提到了一个非常方便的学习工具——The Coding Ground,这是一个在线平台,提供了一个高端专用服务器,使学习者无需本地安装环境就能进行Unix/Linux基本命令和Shell脚本的学习和实践。这是一个完全免费且便捷的...

    《Java 程序员眼中的 Linux》.zip

    这本书的压缩包包含了一份名为"Linux-Tutorial-master"的资源,这很可能是一个完整的Linux教程项目。 在Java开发中,理解Linux的基本概念和命令行操作至关重要。Linux提供了强大的文本处理工具、版本控制系统(如...

    PostgreSQL Tutorial (html)

    总的来说,"PostgreSQL Tutorial (html)"将引导你走过PostgreSQL的世界,结合Linux shell和`awk`,你将具备在实际环境中管理和操作数据库的能力。无论是为了个人项目还是职业发展,掌握PostgreSQL都将对你的IT事业...

    intro2linux:UNIX 和 GNULinux 系统中生物计算的介绍性研讨会

    GNU / Linux系统中的生物计算入门讲习班 欢迎来到 GNU / Linux 系统中的生物计算介绍性研讨会! 研讨会的理由和目标 基因组学工作是在高性能 UNIX 或 GNU/Linux 服务器上完成的。 因此,我建议您在开始学术培训时...

    A Basic UNIX Tutorial

    - **高级命令**:grep、awk、sed等用于文本处理和数据过滤。 #### 五、文件和目录管理 - **创建文件和目录**:使用touch、mkdir等命令。 - **文件操作**:移动、复制、删除文件使用mv、cp、rm等命令。 - **查看...

    Sed Tutorial

    `sed` 本身没有内置的行计数功能,但可以结合其他工具如 `awk` 或 `wc` 来实现这一目的。 **示例**: 使用 `sed` 和 `wc` 来计算文件行数。 ```shell $ sed 'p' thegeekstuff.txt | wc -l 7 ``` 这里,`sed 'p'` ...

Global site tag (gtag.js) - Google Analytics