AWK函数

news/2024/7/7 12:50:38

函数


内置字符串函数表

函数

概述

gsub(r,s)

用s替换r(文本整页)

gsub(r,s,f)

在f中用s替换r(文本整页)  

sub(r,s)

s替换r (单字节)  

sub(r,s,f)

在f中用s替换r(但字节)  

match(r,s)

返回字符串r是否包涵s的字符串

index(r,s)

返回r字符串中s的位置

length(s)

返回字符串s的长度

split(r,s,“fs”)

在fs上将r分成序列s(第几列,数组名,分隔符)

substr(r,s,f)

在r上截取s到f的字符串  

tolower(r)

在r字符串中将所有大写字符更改为小写

toupper(r)

在r字符串中将所有小写字符更改为大写

asort(s,r)

对s数组的值进行排序,并丢弃s的值,赋予b

asorti(s,r)

对数组的下表进行排序,并丢去原来的值

gensub(s,r,f)

指定f的字段数,r替换s

getline [ Variable ] < Expression

将文件内容放入到指定变量,并打印,但每次只读取一行

getline [ variable ]

如果没有指定变量,则$0成为它的值

system()

在awk中调用linux系统命令

gsub函数主要用于在替换整个文本的字符串

[root@localhost ~]# echo "this is a text" | awk'{gsub("t","H");print $0}'

Hhis is a HexH

gsub也可以将字符串定义一个变量,然后在进行替换

[root@localhost ~]# awk 'BEGIN{a="this is atext";gsub("t","H",a);print a}'

Hhis is a HexH

sub替换字符串中的单个第一次出现的字符

[root@localhost ~]# echo "this is a text" | awk'{sub("t","H");print $0}'

Hhis is a text

sub定义变量后替换。

[root@localhost ~]# awk 'BEGIN{a="this is atext";sub("t","H",a);print a}'

Hhis is a text

match主要用于在测试字符串中是否包含s

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=match(a,"x");print s}'

13

index返回r在s中的位置,注意的就是只匹配一次

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=index(a,"t");print s}'

1

length返回字符串的长度

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=length(a);print s}'

14

split在fs上将r分成序列s(第几列,数组名,分隔符)

[root@localhost ~]# awk 'BEGIN{a="this is atext";split(a,c," ");for(i in c){print i,c[i]}}' | sort

1 this

2 is

3 a

4 text

substr在r上截取s到f的字符串

[root@localhost ~]# awk 'BEGIN{a="this is atext";s=substr(a,"1","11");print s }'

this is a t

substr冒上空格也算一个字符,下面来个去除空格,用到gsub

[root@localhost ~]# awk 'BEGIN{a="this is atext";gsub("","",a);s=substr(a,"1","11");print s }'

thisisatext

tolower将所有字符串由大写转换成小写

[root@localhost ~]# awk 'BEGIN{a="THIS IS ATEXT";print tolower(a)}'

this is a text

toupper将所有字符串由小写转换成大写

[root@localhost ~]# awk 'BEGIN{a="this is atext";print toupper(a)}'

THIS IS A TEXT

asort对数组的值进行排序,丢弃原先的值,必须定义数组的值

===== 文本内容 =====

36 98

45 65

12 68

48 93

42 71

[root@localhost ~]# awk'{a[$1]=$2}END{s=asort(a,b);for(i=1;i<=s;i++)print i,b[i]}' cc

1 65

2 68

3 71

4 93

5 98

asorti对数组的下标进行排序,这里没有给数组定义

[root@localhost ~]# awk'{a[$1]}END{s=asorti(a,b);for(i=1;i<=s;i++)print i,b[i]}' cc

1 12

2 36

3 42

4 45

5 48

gensub指定要替换的单个字符串。这个函数比较舒服看下面的用法

[root@localhost ~]# echo '111111' | awk '{printgensub("1","2",6)}'

111112

getline默认读取文件一行,如果不没指定BEGIN,则打印第二行,因为awk在执行的时候已经读取了一行了

[root@localhost www]# awk 'BEGIN{getline d <"cc" ;print d}'

36 98

getline在不指定变量时的操作

[root@localhost www]# awk 'BEGIN{getline <"cc" ;print $0}'

36 98

使用getline打印文件的所有行,必须使用循环语句

[root@localhost www]# awk 'BEGIN{while(getline d <"cc"){print d}}'

36 98

45 65

12 68

48 93

42 71

system函数用来调用系统中的命令

[root@localhost www]# awk 'BEGIN{a=system("ls-l");print a}'

total 20

-rw-r--r--. 1 root root  30 Jan  8 12:02 cc

drwxr-xr-x. 2 root root 4096 Feb 14  2012 cgi-bin

drwxr-xr-x. 3 root root 4096 Nov  1 15:20 error

drwxr-xr-x. 2 root root 4096 Feb 14  2012 html

drwxr-xr-x. 3 root root 4096 Nov  1 15:20 icons

0

本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1637997如需转载请自行联系原作者


qianghong000


http://www.niftyadmin.cn/n/2707836.html

相关文章

oracle里面查找重复项,Oracle数据库查询重复数据及删除重复数据方法

工作中&#xff0c;发现Oracle数据库表中有许多重复的数据&#xff0c;而这个时候老板需要统计表中有多少条数据时(不包含重复数据)&#xff0c;只想说一句MMP&#xff0c;库中好几十万数据&#xff0c;肿么办&#xff0c;无奈只能自己在网上找语句&#xff0c;最终成功解救&am…

P5028 Annihilate

P5028 Annihilate 50个串&#xff0c;任意两两的最长公共子串 回忆最长公共子串求法 1.hash二分 2.SAM 3.SA&#xff0c;属于不同的串的hei的max 1.hash二分 暴力两两枚举再跑的话直接TLE 2.SAM 卡空间64MB跑不过去 3.SA 其实就是两个最长公共子串的扩展 每个i位置&#xff0c;…

oracle sga aix重启,在AIX 5.3+Oracle 10.2.0.4 平台上将SGA PIN在内存中,并使用大页内存...

首先检查AIX版本&#xff0c;修改一些系统参数&#xff1a;[xfxyxh][/]#oslevel -s5300-09-01-0847符合操作系统5.3 ML01以上的要求。接着修改系统参数&#xff1a;[xfxyxh][/]#vmo -p -o v_pinshm1[xfxyxh][/]#vmo -p -o minperm%5[xfxyxh][/]#vmo -p -o maxperm%90[xfxyxh][/…

2017-2018-1 20155319 《信息安全系统设计基础》第九周学习总结

2017-2018-1 20155319 《信息安全系统设计基础》第九周学习总结 教材学习内容总结 1.存储技术 三种常见存储技术&#xff1a;RAM/ROM/磁盘 &#xff08;1&#xff09;随机访问存储器RAM 两类&#xff1a;静态RAM&#xff08;SRAM&#xff09;和动态RAM&#xff08;DRAM&#xf…

卷积神经网络的结构总结

转自&#xff1a; CNN网络架构演进&#xff1a;从LeNet到DenseNet https://www.cnblogs.com/skyfsm/p/8451834.html 基于深度学习的目标检测技术演进&#xff1a;R-CNN、Fast R-CNN、Faster R-CNN https://www.cnblogs.com/skyfsm/p/6806246.html 转载于:https://www.cnblogs.c…

iview form 表单的怪异小BUG

当同一个弹窗中的表单重复利用时&#xff1a; 我原先的代码逻辑是&#xff1a; 1 <Form :label-width"100" class"mt20" ref"changeParam" :rules"ruleValidate" :model"changeParam">2 <Row>3 …

What Is Target Blank Anchor Tag Phishing Attack? How To Prevent It?

Instagram fixed a big issue which is taken for granted by most of the frontend developers around the world. It’s the issue of putting a link with target”_blank” attribute in an anchor tag to make it open in a new tab. There is a problem in how the brows…

oracle headerblock,ORACLE中段的HEADER_BLOCK示例详析

前言段(segment)是一种在数据库中消耗物理存储空间的任何实体(一个段可能存在于多个数据文件中&#xff0c;因为物理的数据文件是组成逻辑表空间的基本物理存储单位)最近在学习段(segment)、区间(extent)时&#xff0c;对段的HEADER_BLOCK有一些疑问&#xff0c;本文记录一下探…