awk使用备忘
来源:互联网 发布:自动分辨率软件 编辑:程序博客网 时间:2024/05/20 12:51
awk的主体块
主体部分的语法要求如下:
/pattern/ {awk-commands}
前段时间有个需求:就是获取db2 list directory的结果中与我的需求数据库名称一致的数据库的一些信息:
[db2inst1@bogon tmp]$ db2 list db directory System Database Directory Number of entries in the directory = 3Database 1 entry: Database alias = DBMDB Database name = DBMDB Local database directory = /home/db2inst1 Database release level = 14.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =Database 2 entry: Database alias = TYDB_161 Database name = TYDB Node name = NODE_161 Database release level = 14.00 Comment = Directory entry type = Remote Catalog database partition number = -1 Alternate server hostname = Alternate server port number =Database 3 entry: Database alias = MYDB Database name = MYDB Local database directory = /home/db2inst1 Database release level = 14.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =
比方说想获得Database alias= MYDB
的其他信息,比如Database name,Local Database directory信息等,当时的处理办法是这样的:
[db2inst1@bogon tmp]$ db2 list db directory| grep 'MYDB' -A 5|grep 'Database name'|awk -F'=' '{print $2}' MYDB[db2inst1@bogon tmp]$ db2 list db directory| grep 'MYDB' -A 5|grep 'Local database directory'|awk -F'=' '{print $2}' /home/db2inst1
grep了两次,还是有点儿繁琐的哈,后面看到了“别人家的”代码是这样处理的:
亮点在这里哦:
DBName=$(db2 list db directory|awk -v dba='MYDB' '/^ Database alias[ ]*=/{if(dba==$NF){flag=1}}/^ Database name[ ]*=/{if(flag==1){printf "%s",$NF;exit}}')DBPath=$(db2 list db directory|awk -v dba='MYDB' '/^ Database alias[ ]*=/{if(dba==$NF){flag=1}}/^ Local database directory[ ]*=/{if(flag==1){printf "%s",$NF;exit}}')
awk的主体块(Body Block)
主体部分的语法要求如下:
/pattern/ {awk-commands}
对于每一个输入的行都会执行一次主体部分的命令。默认情况下,对于输入的每一行,AWK 都会很执行命令。但是,我们可以将其限定在指定的模式中。 注意,在主体块部分没有关键字存在。
所以上面的示例,当匹配到以‘ Database alias’开头的行后,判断他最后一个域是否等于MYDB,如果等于的话,flag设置为1;然后继续匹配下面的行,匹配到‘Database name’开头的行后,判断flag等于1的话,就输出,因为可能下面还会继续匹配到‘ Database name’开头的行,但我们只需要紧相邻的行,所以要记得print完了之后exit。
进阶:
需求: 就是我需要检查db2dart导出的文件的column的顺序,获取Column 1:之后的这行Value,然后输出所有的Value。
Record Type= Table Data Record
Slot 3: Offset Location = 2868 (xB34) Record Length = 48 (x30) Record Type = Table Description Record Number of Columns = 5 Column 1: Type is Long Integer Length = 4 Prohibits NULLs Prohibits Default Fixed offset: 0 Column 2: Type is Variable Length Character String Maximum Length = 20 Allows NULLs Prohibits Default Fixed offset: 4 Fixed offset: 4 Column 3: Type is Small Integer Length = 2 Allows NULLs Prohibits Default Fixed offset: 9 Column 4: Type is Variable Length Character String Maximum Length = 200 Allows NULLs Prohibits Default Fixed offset: 12 Column 5: Type is Fixed Length Character String Length = 4 Allows NULLs Prohibits Default Fixed offset: 17 Slot 4: Offset Location = 2742 (xAB6) Offset Location = 2742 (xAB6) Record Length = 126 (x7E) Record Type = Table Data Record (FIXEDVAR) (PUNC) Record Flags = 0 Fixed part length value = 22 Column 1: Fixed offset: 0 Type is Long Integer Value = 3001 Column 2: Fixed offset: 4 Type is Variable Length Character String Length = 6 Offset = 22 74344C54 5434 t4LTT4 Column 3: Fixed offset: 9 Type is Small Integer Value = 0 Column 4: Fixed offset: 12 Type is Variable Length Character String Length = 90 Offset = 28 54347434 54347434 54347434 54347434 T4t4T4t4T4t4T4t4 54347434 54347434 54347434 74347434 T4t4T4t4T4t4t4t4 74347434 74347434 74346173 6466736B t4t4t4t4t4asdfsk 64666166 61736673 66736466 61736466 dfafasfsfsdfasdf 6A736164 666C6120 73207366 64617364 jsadfla s sfdasd 66736466 61647366 6173 fsdfadsfas Column 5: Fixed offset: 17 Type is Fixed Length Character String 62656E6B benk Slot 5:
上面是文件的部分示例,现在就是要取出这个文件中我们需要的部分。
awk使用案例
示例1:
$(echo "${_output}"|tail -1|awk '{if($1~"^[-,0-9]*$"){printf "%d",$1}else{printf "%d",5}}')
[db2inst1@bogon tmp]$ db2 "force application all"DB20000I The FORCE APPLICATION command completed successfully.DB21024I This command is asynchronous and may not be effective immediately.
示例2
[db2inst1@bogon tmp]$ echo '-1316,-1017'|awk -F, -v c='-1316' '{for(i=1;i<=NF;i++){if($i==c && c!=0){f=1}}}END{if(f==1){printf 100}else{printf "%d",c}}' 100
这个代码不好看懂的话我们整理格式化一下:
awk -F, -v c='-1316' '{for(i=1;i<=NF;i++) {if($i==c && c!=0) {f=1} }}END{if(f==1) {printf 100} else {printf "%d",c} }'
阅读全文
0 0
- awk使用备忘
- awk sed等神奇使用备忘
- awk&sed小备忘
- sed与awk技巧备忘
- awk使用
- AWK使用
- awk使用
- awk使用
- AWK使用
- awk使用
- awk使用
- awk使用
- awk使用
- awk使用
- awk使用
- awk使用
- awk使用
- awk 使用
- 把字符串转换成整数(java版)
- Python中单/双下划线使用
- 909422229_下拉框便利跳转与jsp获取当前时间
- Codeforces 813A The Contest 题解
- Factory Method模式角色与结构
- awk使用备忘
- windows找不到文件“-n”
- Performance Toolkit安装
- md5加密工具类
- laravel依赖注入原理
- 使用新浪微博API的OAuth认证发布微博
- google chrome for linux版本详细列表
- 欢迎使用CSDN-markdown编辑器
- mac系统安装Apache Tomcat的详细步骤