最后更新:2013-11-19, Ver 2.3.9-1119
| wfr - 支持多国语言的字符串批量查找和替换 - 批量字符集编码转换
|
2013-11-19, Ver 2.3.9-1119
2009-12-13, Ver 2.3.6.1213
2009-03-12, Ver 2.3.5.0312
2008-09-09, Ver 2.3.4.0909
2008-09-08, Ver 2.3.3.0908
2008-08-16, Ver 2.3.2.715
2007-01-20, Ver 2.3.1.120
|
| wfr 内部使用完全基于 UNICODE 的高效正则引擎,能够完成各种国际化条件的正则匹配。 以下举例说明:
关于 UNICODE 正则的操作效率,这个引擎比我能找到的所有开源的非 UNICODE 匹配引擎至少快一倍左右(因为所有字符类的匹配都是直接查表映射的,都是标准 O(1) 算法)。 不过这只是匹配引擎的效率,由于所有文件在“匹配->替换”前后都要做一次编码转换,所以典型应用下,wfr 效率比 fr 低一些。wfr 并不是 fr 的升级版,能够用 fr 搞定的任务不推荐用 wfr 完成。 |
wfr 支持分别指定三个参量的字符集编码:
例如:“wfr *.txt *.htm -r:"[иウ]" -t:"狟ね" -encarg:big5 -encin:gbk -encout:utf-8 -s” 把当前目录和所有子目录下的任何 txt 及 htm 文件中的“[你我他她它]们” 都替换为“朋友们”;文件的编码为:gbk,参数使用 big5 指定,替换后将文件格式改为 utf-8 编码保存。 咋一看指定参数编码好像没用,其实不然。例如在异种语言环境下(如在中文系统下操作韩文)的搜索替换;在DOS下使用wfr(DOS的系统默认代码页永远是ASCII)等场合,指定参数编码是很有意义的。 关于字符集转换功能库,如果当前系统搜索路径中存在 GUN 的 libiconv 库(iconv.dll),则优先使用 libiconv,否则使用操作系统自带的字符集转换 API。这样做的考虑如下:
|
|
C:\Documents and Settings\Administrator>
#wfr
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
简单中文帮助
C:\Documents and Settings\Administrator>
#wfr
===============================================================================
wide find and replace Ver 2.3.9-1119 by BaiYang / 2004 - 2013, 免费软件
主页地址 - http://baiy.cn
===============================================================================
在文件或管道中批量搜索-替换字符串——支持多字符集编码
用法: wfr [文件通配符1 文件通配符2 ...] {查找选项:查找内容} {替换选项:替换为}
[其它选项]
===============================================================================
查找选项:
-f: - 匹配精确串
-fic: - 匹配精确串(忽略大小写)
-r: - 正则表达式匹配
-ric: - 正则表达式匹配(忽略大小写)
-rnnl: - 跨行正则匹配
-rnnlic: - 跨行正则匹配(忽略大小写)
-any - 匹配文件或管道中的所有内容,主要用于完成单纯的批量字符集编码转换
===============================================================================
替换选项:
-t: - 将匹配的内容替换为指定字符串
-tu: - 先将指定的“替换为”字符串变为大写,然后开始替换
-tl: - 先将指定的“替换为”字符串变为小写,然后开始替换
注意: 如果您正在使用正则匹配,那么您可以在“替换为”字符串中使用子表达式代换
(\0 ~ \9)
===============================================================================
字符编码选项:
-encin: - 指定输入文本(文件或管道)的字符集编码。
默认值: 使用当前操作系统的默认代码页。
-encout: - 指定输出(结果)文本的字符集编码。
默认值: 与 "-encin" 指定的值相同。
-encarg: - 指定搜索和替换选项的内容使用何种字符集编码。
默认值: 使用当前操作系统的默认代码页。
-unisign - 如果输出指定为 UNICODE 宽字符编码 (如: UCS-2, UTF-8/16 等), 则在文
件头部加入 BOM 签名,这样支持 unicode 的文本编辑器将会自动识别该文
件的编码格式。
-listenc - 列出所有 wfr 支持的字符集编码。
-listcmp - 列出 wfr 认可的键入字符集编码转换规则表。
-force - 强制实施转换,忽略编码兼容性规则。
===============================================================================
其它选项:
-h -? - 当前屏幕
-s - 包含子目录
-exp - 启用内存扩展算法,将会加倍wfr 的内存使用量,但是很多时候可以极大地
提高执行效率。现已默认开启。
-noexp - 禁用内存扩展算法,参考:-exp 选项的说明。
-stdin - 从标准输入设备获得要查找的内容,并将结果从到标准输出设备(如果未指
定任何文件模板的话,则自动启用该选项)
-stdout - 像平时一样, 从文件中获得输入, 但将结果送到标准输出(而不是写回文
件)
-frc - 显示每个文件内发生的替换次数
-trc - 显示所有文件总共发生的替换次数
-errstop - 当遇到文件或子目录访问错误时,终止搜索任务。
默认: 遇到无法访问的子目录访跳过;遇到无法访问的文件时,打印错误
信息并继续
===============================================================================
POSIX 和 Perl 风格的正则表达式:
- "find - replace" 完整支持 POSIX.2 扩展标准及 Perl 风格的正则表达式。
以下是支持的字符类及其对应关系:
POSIX perl 描述
--------------------------------------------------------------------
[:alnum:] 字母和数字
[:alpha:] \a 字母
[:lower:] \l 小写字母
[:upper:] \u 大写字母
[:blank:] 空格和制表符
[:space:] \s 空白字符
[:cntrl:] 控制字符
[:digit:] \d 十进制数字
[:xdigit:] \x 十六进制数字
[:graph:] 可打印字符(不包括空白符)
[:print:] \p 可打印字符(包括空白符)
[:punct:] 标点符号
- 以下是一些 perl 的特殊字符类:
perl POSIX等效 描述
--------------------------------------------------------------------
\o [0-7] 八进制数字
\O [^0-7] 非八进制数字
\w [[:alnum:]_] 单词组成
\W [^[:alnum:]_] 非单词组成
\A [^[:alpha:]] 非字母
\L [^[:lower:]] 非小写字母
\U [^[:upper:]] 非大写字母
\S [^[:space:]] 非空白符
\D [^[:digit:]] 非十进制数字
\X [^[:xdigit:]] 非十六进制数字
\P [^[:print:]] 非可打印字符
\< [^[:alpha:]_] 单词开始
\> [^[:alnum:]_] 单词结束
- 注意: posix 字符类是必须工作在集合中的(“[”和“]”内)。相反,perl 风格
的字符类是工作在集合运算之外的。
- 此外,为了便于在命令行输入一些特殊字符,特别定义了一下别名:
perl风格 POSIX风格 描述
-----------------------------------------------------------------------
\" [:dq:] 双引号
\' [:sq:] 单引号
\t [:tb:] 制表符
\n [:nl:] 换行符 (0x0A)
\r [:rt:] 回车符 (0x0D)
\b [:bs:] 退格符
===============================================================================
TCL 8.2 兼容的高级正则表达式(ARE)
- 为 -r:, -ric:, -rnnl:, -rnnlic: 等命令添加 '***:' 前缀即可开启高级正则表达
式功能。ARE 提供了比 POSIX ERE/BRE 更为强大的正则匹配语法,并且使用一套自定
义的字符类。有关 ARE 的详细信息,请访问:
http://baiy.cn/utils/_regex_doc/index.htm
===============================================================================
程序返回码:
-1 - 执行替换时发生错误
0 - 替换成功完成
1 - 没有可替换的内容(未找到匹配的模板)
===============================================================================
开关的前缀和后缀:
* 所有命令行开关(选项)都是大小写无关的(如: "-fic:" 和 "-FIC:")
* 开关的前缀可以是 "-" 或 "/"(如: "/s" 和 "-s")
* 开关的后缀可以是 ":" 或 "="(如: "/f:", "/f=", "-f:" 和 "-f=" 等效)
===============================================================================
应用示例:
wfr *.txt *.htm -f:teh -t:the
wfr *.txt -fic:"My Dir" -tu:"%ROOTDIR%"
wfr *.cpp *.h -r:"***:(#define.*?)MY_DEF" /t:"HER_DEF"
type server.log | wfr /r:"Connected.*218.79" /t:"**Attack** \0"
type server.log | wfr /r:"^(Smith|James).*\n" /t
unzip -l *.zip | wfr /r:"^(.{22}:\d\d\s*|(A| \a| -|.*files$).*\n)" /t
wfr *.txt /s /any /encin:gbk /encout:utf-8
wfr *.log /any /encin:ucs-2 /encout:gbk /stdout | OtherApp.exe ...
wfr *.txt *.htm -r:"张(\a+/>)" -t:"李\1"
wfr *.txt *.htm -r:"[你我他她它]们" -t:"朋友们"
|
C:\Documents and Settings\Administrator>
#wfr
===============================================================================
wide find and replace Ver 2.3.9-1119 by BaiYang / 2004 - 2013, Freeware
homepage - http://baiy.cn
===============================================================================
Find string in files or pipe, and replace it to another string With Multi-
charset encoding support.
USAGE: wfr [filePattern1 filePattern2 ...] {FindOption:StringToFind}
{ReplaceOption:StringToReplacement} [OtherOptions]
===============================================================================
FIND OPTIONs:
-f: - find
-fic: - find ignore case
-r: - regular expresion find
-ric: - regular expresion find ignore case
-rnnl: - regular expresion find ignore newline
-rnnlic: - regular expresion find ignore newline ignore case
-any - match anything in file, usually use to performing text encoding
convertion
===============================================================================
REPLACE OPTIONs:
-t: - change the match to"StringToReplacement"
-tu: - make the "StringToReplacement" upper case, and change the match
to it.
-tl: - make the "StringToReplacement" lower case, and change the match
to it.
NOTE: If find option you specified is a regular expresion method, the replace
option will support sub expressions (\0-\9) as well.
===============================================================================
CHARSET ENCODING OPTIONs:
-encin: - specify charset encoding for the input text (file or pipe).
DEFAULT: use current system's default codepage.
-encout: - specify charset encoding for the output text.
DEFAULT: same as "-encin".
-encarg: - specify charset encoding of the find and replace options.
DEFAULT: current system's default codepage.
-unisign - if the output encoding is unicode (i.e. UCS-2, UTF-8/16, etc.),
then add BOM signature to the file.
-listenc - list all accepted charset encoding names.
-listcmp - list all compatible encoding convertion combination.
-force - enforce the text encoding convertion specified by "-encin" and
"-encout", even if it is not compatible.
===============================================================================
OTHER OPTIONs:
-h -? - Is screen
-s - include sub folders
-exp - enable the memory expand algorithm, will double the memory usage,
but MUCH quick in many case. Now Default.
-noexp - disable the memory expansion algorithm. see also: -exp.
-stdio - get input from standard input device (keyboard and pipe),
and put the results to standard output device.
default when file pattern is omitted.
-stdout - get input from file(s) as normally, but put the results to
standard output device.
-frc - show File Replacements Count
-trc - show Total Replacements Count
-errstop - stop on a file or sub-directory access error (the old behave).
DEFAULT: ignore the access error and try to continue.
===============================================================================
POSIX and perl style Regular Expression:
- "find - replace" fully support POSIX.2 Extended and Perl style Regular
Expresion. Here is a list of they character classes:
POSIX perl Description
--------------------------------------------------------------------
[:alnum:] letters and digits
[:alpha:] \a letters
[:lower:] \l lowercase letters
[:upper:] \u uppercase letters
[:blank:] space and tab characters
[:space:] \s whitespace characters
[:cntrl:] control characters
[:digit:] \d decimal digits
[:xdigit:] \x hexadecimal digits
[:graph:] printable characters excluding space
[:print:] \p printable characters including space
[:punct:] punctuation characters
- And here a some special char classes in perl:
perl POSIX equivalent Description
--------------------------------------------------------------------
\o [0-7] octal digit
\O [^0-7] non-octal digit
\w [[:alnum:]_] word character
\W [^[:alnum:]_] non-word character
\A [^[:alpha:]] non-alphabetic character
\L [^[:lower:]] non-lowercase character
\U [^[:upper:]] non-uppercase character
\S [^[:space:]] non-whitespace character
\D [^[:digit:]] non-digit
\X [^[:xdigit:]] non-hex digit
\P [^[:print:]] non-printable characters
\< [^[:alpha:]_] begin of word
\> [^[:alnum:]_] end of word
- note: posix char class must working in the square brackets. contrary,
perl's must stay outside of the brackets.
- And several alias has been created to help input some special char:
perl style POSIX style Description
-----------------------------------------------------------------------
\" [:dq:] double quotation
\' [:sq:] single quotation
\t [:tb:] table
\n [:nl:] new line (0x0A)
\r [:rt:] return (0x0D)
\b [:bs:] backspace
NOTE: the posix style alias also available on /t, /tu and /tl when using
the regex ("/r" and "/ric") match.
===============================================================================
TCL 8.2 Compatibal ARE expression:
- Use '***:' prefix with -r:, -ric:, -rnnl: or -rnnlic: to enable Advanced
Regular Expression mode. The ARE mode have more powerful expression then
POSIX BRE and ERE rule. And it has it's own char classes. More info at:
http://baiy.cn/utils/_regex_doc/index.htm
===============================================================================
RETURN CODE:
-1 - error occured when performing replacement.
0 - replacement has been done successfully.
1 - nothing to replace (pattern not found).
===============================================================================
SWITCH PREFIX and SUFFIX:
* All switchs (options) are case-insensitive (i.e: "-fic:" or "-FIC:")
* Switch Prefix can be either "-" or "/" (i.e: "/s" or "-s")
* Switch Suffix can be either ":" or "=" (i.e: "/f:", "/f=", "-f:" or "-f=")
===============================================================================
EXAMPLEs:
wfr *.txt *.htm -f:teh -t:the
wfr *.txt -fic:"My Dir" -tu:"%ROOTDIR%"
wfr *.cpp *.h -r:"***:(#define.*?)MY_DEF" /t:"\1HER_DEF"
type server.log | wfr /r:"Connected.*218.79" /t:"**Attack** \0"
type server.log | wfr /r:"^(Smith|James).*\n" /t
unzip -l *.zip | wfr /r:"^(.{22}:\d\d\s*|(A| \a| -|.*files$).*\n)" /t
wfr *.txt /s /any /encin:gbk /encout:utf-8
wfr *.log /any /encin:ucs-2 /encout:gbk /stdout | OtherApp.exe ...
wfr *.txt *.htm -r:"
|
wfr 支持的字符集:使用 -listenc 参数可以查看 wfr 支持的字符集编码列表如下:
字符集编码使用名称或代码页指定均可,并且不区分大小写。
如果用户确实能保证正在进行有意义的转换,可以使用 “/force” 参数要求 wfr 忽略字符集编码兼容性检查。 |
安装步骤:
注意事项:
|
| wfr.rar(Windows/DOS)iconv.rar(可选)icu.rar(可选) wfr_linux_x86.zip (在 Ubuntu 8.04 上测试通过) wfr_linux_x64.zip (在 Ubuntu 8.04 上测试通过) wfr_freebsd_x64.zip(在 FreeBSD 7.0 上测试通过) wfr_netbsd_x86.zip (在 NetBSD 5.0.1 上测试通过) wfr_solaris_x86.zip(在 OpenSolaris 2009.06 上测试通过) |