linux command Line and shell script bible(笔记十六)
来源:互联网 发布:中泰铁路 知乎 编辑:程序博客网 时间:2024/06/13 15:05
21.4.1 if语句
1.if (condition)
statement
$ gawk '{if ($1 > 20) print $1}' data
2.
if (condition) statement
$ gawk '{
>if ($1 > 20)
>{
>x = $1 * 2
>print x
>}
>}' data
$ gawk '{
>if ($1 > 20 )
>{
>xxxxxxxxxxxxx
> } else
>{
>XXXXXXXXXXXXXXXXXXXXXXX
>}}' data
或者: if (condition) statement1; else statement2
21.4.2 while语句
while (condition)
{
statements
}
do
{
statements
} while (condition)
支持c语言风格的for循环
for( variable assignment; condition; iteration process)
21.5 格式化打印:C语言中的printf命令的用法一样
printf "format string", var1, var2 ...
format string指定格式:%[modifier]control-letter
如:printf "The test is: %e\n",x
注意:需要在printf命令的末尾手动添加换行符,要不会继续打印在同一行。
$ gawk 'BEGIN{FS="\n";RS=""} {printf "%-13s %s\n", $1,$4}' data2
注意:默认情况下是右对齐,加上减号就左对齐,加数字就是输出采用多少位字符。
21.7自定义函数
function name([variables])
{
statements
}
#函数名必须能够唯一标识函数,可以在gawk程序中传给函数一个或者多个变量
#函数还能用return语句返回值。
#在定义函数时,必须出现增强爱所有代码块之前(包括BEGIN代码块)。
#使用函数:function_name()
21.7.3创建函数库,要使用包括很多函数代码的函数库,需要用-f命令行参数,并且不能将-f命令行参数和内联gawk脚本放在一起使用。
注意:(就是说在程序脚本文件不能与函数库文件放在同一个-f后)
$ gawk -f funclib -f script data (funclib 和script不能放在一个-f参数之后)
- linux command Line and shell script bible(笔记十六)
- linux command Line and shell script bible(笔记四)
- linux command Line and shell script bible(笔记五)
- linux command Line and shell script bible(笔记七)
- linux command Line and shell script bible(笔记八)
- linux command Line and shell script bible(笔记九)
- linux command Line and shell script bible(笔记十)
- linux command Line and shell script bible(笔记十一)
- linux command Line and shell script bible(笔记十二)
- linux command Line and shell script bible(笔记十三)
- linux command Line and shell script bible(笔记十四)
- linux command Line and shell script bible(笔记十五)
- linux command Line and shell script bible(笔记十七)
- linux command Line and shell script bible(笔记十八)
- linux command Line and shell script bible(笔记十九)
- linux command Line and shell script bible(笔记一_文档/文件系统)
- linux command Line and shell script bible(笔记二_非用户变量)
- linux command Line and shell script bible(笔记三_权限/用户)
- Plist文件详解
- 程序员之路——学习技术的方法
- ios消息推送机制原理与实现
- PROC程序如何编译时不需要连接数据库校验?
- VC 运行时库 /MD、/MDd 和 /MT、/MTd
- linux command Line and shell script bible(笔记十六)
- c++训练营---继承,多态,重载
- Linux family tree
- 矢量图形(vector graphics)和位图图像(bitmap)以及分辨率概念
- 【分享】900部非常出名的电影信息数据集(Information for over 900 very popular movies)
- HDU 1754 I Hate It(线段树求区间最大值)
- WebService服务的三种途径Endpoint Disco WSDL 有什么不同
- wsdl:binding中 style: "document" 和 RPC 区别
- S3C6410开发之ADC(一)