[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