-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawk.tex
More file actions
58 lines (38 loc) · 1.6 KB
/
Copy pathawk.tex
File metadata and controls
58 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
\input{../common/preambleconf.tex}
\title{AWK笔记}
\input{../common/author.tex}
\date{09-26-2012}
\begin{document}
\maketitle
AWK是一门脚本语言,具体历史不再赘述。目前比较知名的三个
实现是nawk,gawk和mawk。其中mawk号称是速度最快的,不过好久
没有更新了\footnote{http://invisible-island.net/mawk/}。这里面主要涉
及到gawk(GNU/Linux, FreeBSD, NetBSD)和nawk(NetBSD,
FreeBSD)\footnote{命令行或者代码中的awk,在不同的系统上链接为
不同变种。如果是不同变种的特性,会用对应的变种名指出}。
相比于sed,AWK更善于处理列,更精确的说是字段(field)。
\section{AWK的基本用法}
\input {../common/awkconf_nolineno.tex}
AWK代码是由单引号(`')和大括号(\{\})括起来的。下面是一个非
常简单的例子\footnote{我是喜欢bash及zsh的。但却喜欢csh
的默认的命令提示符\%}。:\\
\begin{lstlisting}[title={简单例子},abovecaptionskip=0.5cm]
%echo one two three | awk '{print $2}'
two
\end{lstlisting}
\$2为AWK内置的变量,表示第二个字段(从1开始数)。\$0表示整个记录(record)。
下面是常见的内置变量\footnote{详细可以参看awk在线手册}:\\
\begin{tabular}{| c | c |}
\hline
\$0 & 整个记录(record)\\
\hline
\$1,\$n & 第1,n个字段(filed)\\
\hline
NF & 当前记录字段的个数(number of filed)\\
\hline
NR & 目前为止遇到的总记录数(number of record)\\
\hline
FS & 字段分隔符\\
\hline
\end{tabular}
\end{document}