免费注册 查看新帖 |

Chinaunix

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

『求助』文件名匹配问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-25 12:04 |只看该作者 |正序浏览
有如下文件:
CHARBASE046.MYD
CHARBASE046.MYI
CHARBASE047.frm
CHARBASE047.MYD
CHARBASE047.MYI
CHARBASE048.frm
CHARBASE048.MYD
CHARBASE048.MYI
CHARBASE049.frm
CHARBASE049.MYD
CHARBASE049.MYI
CHARBASE.frm
CHARBASE.MRG

要将其中CHARBASE.* 和 CHARBASE049.*所有文件拷贝到其他目录,
请问在命令行下怎样用正则表达式匹配这5个文件名,如果不考虑使用管道,应该怎么写这个命令?
谢谢

论坛徽章:
0
14 [报告]
发表于 2005-12-01 16:54 |只看该作者
老大,我终于懂了。  非常感谢!!!

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
13 [报告]
发表于 2005-11-29 19:00 |只看该作者
这个不是正则表达式,是大概括扩展
使用shell的-x选项观察一个就知道发生了什么

  1. root@sarge:~# set -vx
  2. set -vx
  3. root@sarge:~# echo {1,2}
  4. echo {1,2}
  5. + echo 1 2
  6. 1 2
  7. root@sarge:~# echo {1,2}{a,b,c}
  8. echo {1,2}{a,b,c}
  9. + echo 1a 1b 1c 2a 2b 2c
  10. 1a 1b 1c 2a 2b 2c
  11. root@sarge:~# echo {1,2}\ {a,b,c}
  12. echo {1,2}\ {a,b,c}
  13. + echo '1 a' '1 b' '1 c' '2 a' '2 b' '2 c'
  14. 1 a 1 b 1 c 2 a 2 b 2 c
  15. root@sarge:~# echo {1,2} {a,b,c}
  16. echo {1,2} {a,b,c}
  17. + echo 1 2 a b c
  18. 1 2 a b c
复制代码

论坛徽章:
0
12 [报告]
发表于 2005-11-29 18:41 |只看该作者
要是文件如下
CHARBASE046.MYD
CHARBASE046.MYI
CHARBASE047.frm
CHARBASE047.MYD
CHARBASE047.MYI
CHARBASE048.frm
CHARBASE048.MYD
CHARBASE049049.MYI
CHARBASE049.frm
CHARBASE049.MYD
CHARBASE049.MYI
CHARBASE.frm
CHARBASE.MRG

用正则表达式一次匹配复制上面6个蓝色文件名,该怎么写命令啊
----------------------------------------
我是这么写的
    cp ../Titan/CHARBASE{,049}{,049}.* .
确实可以copy成功,但是系统提示警告:
cp: warning: source file `../Titan/CHARBASE049.MYD' specified more than once
cp: warning: source file `../Titan/CHARBASE049.MYI' specified more than once
cp: warning: source file `../Titan/CHARBASE049.frm' specified more than once
还是不清楚 , . * 具体指待的意思

论坛徽章:
0
11 [报告]
发表于 2005-11-29 17:44 |只看该作者
帮助里面还是没有讲很清楚。

[ 本帖最后由 另一只 于 2005-11-29 18:38 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2005-11-29 17:43 |只看该作者
Brace Expansion
       Brace expansion is a mechanism by which arbitrary strings may be gener-
       ated.  This mechanism is similar to pathname expansion, but  the         file-
       names generated need not exist.        Patterns to be brace expanded take the
       form of an optional preamble, followed by a series  of  comma-separated
       strings        between         a pair of braces, followed by an optional postscript.
       The preamble is prefixed to each string contained  within  the  braces,
       and the postscript is then appended to each resulting string, expanding
       left to right.

       Brace expansions may be nested.        The results of        each  expanded        string
       are  not         sorted;  left        to  right  order  is  preserved.  For example,
       a{d,c,b}e expands into `ade ace abe'.

       Brace expansion is performed before any other expansions, and any char-
       acters  special to other expansions are preserved in the result.         It is
       strictly textual.  Bash does not apply any syntactic interpretation  to
       the context of the expansion or the text between the braces.

       A  correctly-formed  brace  expansion must contain unquoted opening and
       closing braces, and at  least  one  unquoted  comma.   Any  incorrectly
       formed  brace expansion is left unchanged.  A { or , may be quoted with
       a backslash to prevent its being considered part of a brace expression.
       To  avoid conflicts with parameter expansion, the string ${ is not con-
       sidered eligible for brace expansion.

       This construct is typically used as shorthand when the common prefix of
       the strings to be generated is longer than in the above example:

              mkdir /usr/local/src/bash/{old,new,dist,bugs}
       or
              chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}

       Brace  expansion         introduces  a        slight incompatibility with historical
       versions of sh.        sh does not treat opening or closing braces  specially
       when  they  appear as part of a word, and preserves them in the output.
       Bash removes braces from words as a  consequence         of  brace  expansion.
       For  example,  a word entered to sh as file{1,2} appears identically in
       the output.  The same word is output as file1 file2 after expansion  by
       bash.   If strict compatibility with sh is desired, start bash with the
       +B option or disable brace expansion with the +B option to the set com-
       mand (see SHELL BUILTIN COMMANDS below).

论坛徽章:
0
9 [报告]
发表于 2005-11-29 15:48 |只看该作者
谢谢楼上兄弟

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2005-11-26 12:58 |只看该作者
man bash,见 Brace Expansion 段的描述

论坛徽章:
0
7 [报告]
发表于 2005-11-26 10:57 |只看该作者
兄弟们,麻烦指点一下,发现下面的命令也可以:
cp  CHARBASE{,049}.** otherpath/     就是弄不明白, . * 的意思

引申一下:
要是文件如下
CHARBASE046.MYD
CHARBASE046.MYI
CHARBASE047.frm
CHARBASE047.MYD
CHARBASE047.MYI
CHARBASE048.frm
CHARBASE048.MYD
CHARBASE049049.MYI
CHARBASE049.frm
CHARBASE049.MYD
CHARBASE049.MYI
CHARBASE.frm
CHARBASE.MRG

用正则表达式一次匹配复制上面6个蓝色文件名,该怎么写命令啊


[ 本帖最后由 另一只 于 2005-11-29 18:26 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2005-11-25 14:15 |只看该作者
多谢各位帮助 感激不尽啊。
想了很久就是不知道写。

waker 的用法很经典,刚才用了果然可以。
原帖由 waker 于 2005-11-25 12:33 发表
cp  CHARBASE{,049}.* otherpath/


可否解释一下{,049}中逗号的用法,以前从来没有看到过这个元字符,孤陋寡闻,还望点拨一下。




另外{,049}.* 中那个.号在这里到时普通的字符还是匹配符,*号在这里到底是普通意思上的匹配还是正则表达式中的匹配?  看着这.*越看越糊涂。

[ 本帖最后由 另一只 于 2005-11-25 15:26 编辑 ]
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP