免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2281 | 回复: 7
打印 上一主题 下一主题

[文本处理] 1234 awk : gensub [复制链接]

论坛徽章:
6
程序设计版块每日发帖之星
日期:2016-05-11 06:20:00操作系统版块每日发帖之星
日期:2016-05-12 06:20:00每日论坛发贴之星
日期:2016-05-12 06:20:00操作系统版块每日发帖之星
日期:2016-05-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-31 06:20:00每日论坛发贴之星
日期:2016-05-31 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-05-24 18:35 |只看该作者 |倒序浏览
# [ /home/soio/1bs/awks ] {2018-05-24 18:30:29}
: 1527157829:0;➜  echo "a b c \n1 2 3"|awk '{gensub(/2/, "100", "g", $2);print $0}'
a b c
1 2 3


# [ /home/soio/1bs/awks ] {2018-05-24 18:30:37}
: 1527157837:0;➜  echo "a b c \n1 2 3"|awk '{d=gensub(/2/, "100", "g", $2);print d}'
b
100


# [ /home/soio/1bs/awks ] {2018-05-24 18:30:58}
: 1527157858:0;➜  echo "a b c \n1 2 3"|awk '{d=gensub(/2/, "100", "g", $0);print d}'
a b c
1 100 3


# [ /home/soio/1bs/awks ] {2018-05-24 18:31:09}
: 1527157869:0;➜  echo "a b c \n1 2 3"|awk '{d=gensub(/2/, "100", "g");print d}'
a b c
1 100 3


第 1 种情况最不懂

论坛徽章:
4
15-16赛季CBA联赛之青岛
日期:2018-07-09 14:17:2815-16赛季CBA联赛之八一
日期:2018-08-06 15:30:0515-16赛季CBA联赛之广东
日期:2018-08-09 09:11:2115-16赛季CBA联赛之佛山
日期:2019-02-14 09:26:31
2 [报告]
发表于 2018-05-24 19:10 |只看该作者
gensub(regexp, replacement, how [, target]) #
Search the target string target for matches of the regular expression regexp. If how is a string beginning with ‘g’ or ‘G’ (short for “global”), then replace all matches of regexp with replacement. Otherwise, how is treated as a number indicating which match of regexp to replace. If no target is supplied, use $0. It returns the modified string as the result of the function and the original target string is notchanged.

论坛徽章:
6
程序设计版块每日发帖之星
日期:2016-05-11 06:20:00操作系统版块每日发帖之星
日期:2016-05-12 06:20:00每日论坛发贴之星
日期:2016-05-12 06:20:00操作系统版块每日发帖之星
日期:2016-05-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-31 06:20:00每日论坛发贴之星
日期:2016-05-31 06:20:00
3 [报告]
发表于 2018-05-24 19:56 |只看该作者
本帖最后由 1cpuer 于 2018-05-24 20:15 编辑

http://blog.163.com/njut_wangjia ... 425201611100261205/

[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"g1",t)}'
hewwo,world!hewwo,awk!hewwo linux!
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"G1",t)}'
hewwo,world!hewwo,awk!hewwo linux!

g -and G 作何解
echo "123"| sed "s/2/x/g" # g:全局的意思
echo "123"| sed "s/2/x/G" # G?

论坛徽章:
6
程序设计版块每日发帖之星
日期:2016-05-11 06:20:00操作系统版块每日发帖之星
日期:2016-05-12 06:20:00每日论坛发贴之星
日期:2016-05-12 06:20:00操作系统版块每日发帖之星
日期:2016-05-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-31 06:20:00每日论坛发贴之星
日期:2016-05-31 06:20:00
4 [报告]
发表于 2018-05-25 10:48 |只看该作者
本帖最后由 1cpuer 于 2018-05-25 10:50 编辑

buhuile ----------------
ok
--------------------------
如何保存 下面的 ok

ok.ok.png (18.28 KB, 下载次数: 12)

ok.ok.png

ok.ok.png

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-10-11 06:20:0015-16赛季CBA联赛之山东
日期:2016-05-28 18:18:5615-16赛季CBA联赛之新疆
日期:2017-04-12 22:55:4715-16赛季CBA联赛之青岛
日期:2017-06-26 18:30:0315-16赛季CBA联赛之四川
日期:2017-09-04 12:27:0315-16赛季CBA联赛之福建
日期:2018-02-09 14:28:3315-16赛季CBA联赛之同曦
日期:2018-04-17 12:43:3415-16赛季CBA联赛之浙江
日期:2018-07-14 13:27:4015-16赛季CBA联赛之吉林
日期:2018-09-13 15:48:2915-16赛季CBA联赛之新疆
日期:2016-05-07 05:05:3215-16赛季CBA联赛之八一
日期:2016-03-14 12:32:06程序设计版块每日发帖之星
日期:2015-12-12 06:20:00
5 [报告]
发表于 2018-05-28 09:21 |只看该作者
  1. [root@test-01 ~]# echo -e "a b c \n1 2 3"|awk '{$2=gensub(/2/, "100", "g", $2);print $0}'
  2. a b c
  3. 1 100 3
复制代码

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
6 [报告]
发表于 2018-05-29 14:26 |只看该作者
回复 3# 1cpuer


man sed
g G    Copy/append hold space to pattern space.


# echo -e "123\n456"| sed "s/2/x/;G"
1x3

456

#

论坛徽章:
4
15-16赛季CBA联赛之青岛
日期:2018-07-09 14:17:2815-16赛季CBA联赛之八一
日期:2018-08-06 15:30:0515-16赛季CBA联赛之广东
日期:2018-08-09 09:11:2115-16赛季CBA联赛之佛山
日期:2019-02-14 09:26:31
7 [报告]
发表于 2018-05-29 15:21 |只看该作者
  1. seq 3 | sed '1!G;h;$!d'
复制代码

sed 那个 hold space 不好玩,费脑子
h H g G,小写是覆盖,大写是追加,就算hold space,没东西,追加还是追加,楼上那两个空行,就是hold space追加出来的

论坛徽章:
6
程序设计版块每日发帖之星
日期:2016-05-11 06:20:00操作系统版块每日发帖之星
日期:2016-05-12 06:20:00每日论坛发贴之星
日期:2016-05-12 06:20:00操作系统版块每日发帖之星
日期:2016-05-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-31 06:20:00每日论坛发贴之星
日期:2016-05-31 06:20:00
8 [报告]
发表于 2018-05-29 17:03 |只看该作者
本帖最后由 1cpuer 于 2018-05-29 17:11 编辑

# [ /home/soio/1bs/awks ] {2018-05-29 17:02:24}
: 1527584544:0;➜  man awk | grep -n '\<G\>'
871:       %g, %G  Use %e or %f conversion, whichever is shorter, with nonsignificant zeros suppressed.  The %G format uses %E instead of %e.
896:              For %e, %E, %f and %F, the result always contains a decimal point.  For %g, and %G, trailing zeros are not removed from the result.
907:              to the right of the decimal point.  For the %g, and %G formats, it specifies the maximum number of significant digits.  For the %d, %i,  %o,  %u,  %x,
988:       gensub(r, s, h [, t])   Search the target string t for matches of the regular expression r.  If h is a string beginning with g or G, then replace all matches

man awk | grep -A6 '\<G\>' ## 正好找到  gensub(r, s, h [, t])

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP