awk 的怪异现象以及其解决之道:正则表达式范围与语言环境的悲伤故事

本文探讨了AWK中正则表达式与语言环境之间的关系,通过对比不同版本的AWK在处理字符串时的表现,揭示了语言环境对正则表达式的影响。文章最终验证了在新版本的AWK中,该问题已被解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[root@centos ~]# echo abcABC | /bin/gawk '{gsub(/([a-z])/, "x"); print $0}'
xxxxxx
[root@centos ~]# echo abcABC | /bin/gawk '{gsub(/([[:lower:]])/, "x"); print $0}' 
xxxABC

问题1:神奇了,[a-z]不能表示小写了! 咋回事儿呢?

[root@centos ~]# /bin/gawk 'BEGIN{match("womasdRDfadfasKNd",/[A-Z][A-Z]+/);print RSTART,RLENGTH}'
1 3

问题2:输出怎么是1 3,按道理不应该是7 2么?

往下看:

[root@centos ~]# /bin/gawk --version
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
[root@centos ~]# 
[root@centos ~]# echo $LANG
en_US.UTF-8
[root@centos ~]# echo abcABC | /bin/gawk '{gsub(/[a-z]/,"x");print $0}'
xxxxxx
[root@centos ~]# export LANG=C 
[root@centos ~]# echo abcABC | /bin/gawk '{gsub(/[a-z]/,"x");print $0}'
xxxABC
[root@centos ~]# 
[root@centos ~]# 

以上是3.1.5版本的结果。

CU上经过了一番激烈的讨论,终于发现了问题之所在,原来以上问题原来是awk的正则表达式与语言环境的一个悲伤的故事导致的,请阅读:

http://www.gnu.org/software/gawk/manual/html_node/Ranges-and-Locales.html

这个英文文档只看懂了个大概,里边儿说awk在4.0版本中已经解决了这个问题!于是我下载了新版本进行测试,果然OK了!

[root@centos ~]# /usr/local/bin/gawk --version
GNU Awk 4.1.0, API: 1.0
Copyright (C) 1989, 1991-2013 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
[root@centos ~]# 
[root@centos ~]# echo $LANG
en_US.UTF-8
[root@centos ~]# 
[root@centos ~]# /usr/local/bin/gawk '{gsub(/[a-z]/,"x");print $0}'              

[root@centos ~]# echo abcABC | /usr/local/bin/gawk '{gsub(/[a-z]/,"x");print $0}'
xxxABC
[root@centos ~]# 
[root@centos ~]# 
[root@centos ~]# 
以上是4.1.0版本的,很显然是正确的了!

参考:

1. http://bbs.chinaunix.net/thread-4065242-1-1.html

2. http://bbs.chinaunix.net/thread-4103721-1-1.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值