awk实战6-函数系列-字符串函数说明-asort和sub

awk一个神奇而又强大的功能,绝对是一个开发利器,效率直选!希望这里的一些总结能帮到那些存在困惑的人!

awk实战1-基础语法说明

awk实战2-流程控制语句总结

awk实战3-awk数组技巧

awk实战4-函数系列-算数函数说明

awk实战5-函数系列-基本字符串函数

awk实战6-函数系列-字符串函数说明-asort和sub

awk实战7-函数系列-时间函数

awk实战8-函数系列-字节操作

awk实战9-IO函数-getline和close

awk实战10-IO函数-其他介绍

awk实战11-进阶-10种awk有效应用实战

awk实战12-进阶-再谈awk匹配模式

awk处理小问题-解决局部jar包替换

awk字符串函数说明–asort sub

本文主要介绍awk字符串及数组处理的相关函数,这是整个awk系列中最重要的一个部分,很多awk函数可以极大的简化我们开发的需求!-建议优先掌握!

推荐指数:☆ ☆ ☆ ☆ ☆ ☆

1 asort-asorti-关注指数:☆ ☆ ☆ ☆ ☆

asort系列函数是对数组进行排序的一个利器,一个针对键一个针对值,对于数组排序很有意义!

assort排序说明:对于数组的值进行排序,并且丢掉原先键值;

#### 1-未排序前:
user@user:~$ echo -e "12 34 \n78 90 \n56 67 " > test
user@user:~$ awk '{a[$1]=$2}END{for(i in a) print i,a[i]}' test
12 34                                                                      
56 67                                                          
78 90                                                          
#### 2-使用assort排序后:
user@user:~$ awk '{a[$1]=$2}END{slen=asort(a,b);for(i=1;i<=slen;i++)print i"\t"a[i]"\t"b[i]}' test
1	  	34
2	  	67
3	  	90

assorti:返回源数组的元素个数,对数组的下标进行排序;返回结果为数组的键值,原先的数组丢失;

user@user:~$ awk '{a[$1]=$2}END{slen=asorti(a,b);for(i=1;i<=slen;i++)print i"\t"a[i]"\t"b[i]}' test
1		12
2		56
3		78

2 gensub、gsub和sub -关注指数:☆ ☆ ☆ ☆ ☆ ☆

sub系列函数非常实用,对于匹配正则表达式进行替换操作极为方便!

sub( Ere, Repl, [ In ] )

用 Repl 参数指定的字符串替换 In 参数指定的字符串中的由 Ere 参数指定的扩展正则表达式的第一个具体值。sub 函数返回替换的数量。出现在 Repl 参数指定的字符串中的 &(和符号)由 In 参数指定的与 Ere 参数的指定的扩展正则表达式匹配的字符串替换。如果未指定 In 参数,缺省值是整个记录($0 记录变量)

用例如下:

#### 1-sub函数测试
fwd@fwd:~$ awk 'BEGIN{info="this is a 1234 test!"; sub(/[0-9]+/,"abcd",info);print info}'
this is a abcd test!

gsub( Ere, Repl, [ In ] ):gsub使用和sub基本一致,但是它将匹配所有匹配正则表达式的参数而不是第一个!!!

#### 0-准备数据
user@user:~$ echo -e "Hello! Hello! this is 1th line! \nHello! This is 2th line!" > test
user@user:~$ cat test
Hello! Hello! this is 1th line! 
Hello! This is 2th line!
#### 1-使用sub仅替换了第一个数据
user@user:~$ awk '{var=sub(/Hello/,"Hey!");print var"--"$0}' test
1--Hey!! Hello! this is 1th line! 
1--Hey!! This is 2th line!
#### 2-gsub替换了所有的正则表达式匹配数据
user@user:~$ awk '{var=gsub(/Hello/,"Hey!");print var"--"$0}' test
2--Hey!! Hey!! this is 1th line! 
1--Hey!! This is 2th line!

gensub是一个更加灵活的函数–指定匹配中的第n次修改

#### 3-1-gensub替换了匹配的正则表达式数据的第n次进行替换
user@user☆ :~$ awk '{var=gensub(/Hello/,"Hey!",2);print var"--"$0}' test
Hello! Hey!! this is 1th line! --Hello! Hello! this is 1th line! 
Hello! This is 2th line!--Hello! This is 2th line!
user@user☆ :~$ 
#### 3-2-这里的字符串也可以不是默认字符串
user@user☆ :~$ awk 'BEGIN{d1="hi hi ho ha hi hi";var=gensub(/hi/,"Hey!",3,d1);print var"\n"d1}' test
hi hi ho ha Hey! hi
hi hi ho ha hi hi

三个函数对比一下:

user@user☆ :~$ awk 'BEGIN{d1="hi hi ho ha hi hi";var=sub(/hi/,"Hey!",d1);print var"\n"d1}' test
1
Hey! hi ho ha hi hi
user@user☆ :~$ awk 'BEGIN{d1="hi hi ho ha hi hi";var=gsub(/hi/,"Hey!",d1);print var"\n"d1}' test
4
Hey! Hey! ho ha Hey! Hey!
user@user☆ :~$ awk 'BEGIN{d1="hi hi ho ha hi hi";var=gensub(/hi/,"Hey!",3,d1);print var"\n"d1}' test
hi hi ho ha Hey! hi
hi hi ho ha hi hi

这里也给出英文说明:

       gensub(r, s, h [, t])   Search the target string t for matches of the regu‐
                               lar expression r.  If h is a string beginning  with
                               g or G, then replace all matches of r with s.  Oth‐
                               erwise, h is a number indicating which match  of  r
                               to  replace.  If t is not supplied, use $0 instead.
                               Within the replacement text  s,  the  sequence  \n,
                               where  n  is  a  digit  from 1 to 9, may be used to
                               indicate just the text that matched the n'th paren‐
                               thesized subexpression.  The sequence \0 represents
                               the entire matched text, as does the  character  &.
                               Unlike  sub()  and  gsub(),  the modified string is
                               returned as the result of  the  function,  and  the
                               original target string is not changed.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值