免费注册 查看新帖 |

Chinaunix

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

[文本处理] 对通配符{ }的疑惑,求大神帮忙 [复制链接]

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-09-13 17:07 |只看该作者 |倒序浏览
1,本人刚开始学习shell,在看到shell命令执行顺序的时候有如下疑惑,执行顺序第4步,将{ }展开,第十步,进行通配符替换,那{}算是通配符吗?
我看到很多文章都是把{ }算做通配符的,说通配符有*,?,[ ],{ }

2,假如{ }属于通配符的话,按照通配符的定义,是在计算机目录下进行搜索匹配,那么如果文件不存在,也就是搜索不出来的,但如下用法中,{ }是什么用法呢?

[root@newer-test1-4ca6f235-515f-4cac-b2de-db2fa6037d90 11]# touch {abc,efg,xyz}.txt
[root@newer-test1-4ca6f235-515f-4cac-b2de-db2fa6037d90 11]# ls
abc.txt  efg.txt  xyz.txt

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
2 [报告]
发表于 2014-09-13 17:17 |只看该作者
补充一下,我所谓的shell命令执行顺序,是指shell命令在执行之前扫描处理的过程,包括按shell元字符分割命令行,检查第一个词是否为关键字,检查第一个词是否为命令别名,将{ }展开,替换~,变量替换,命令替换,运算替换,将处理后的结果按IFS分割,通配符替换,将第一个词作为命令尝试执行,重定向I/0

论坛徽章:
3
丑牛
日期:2014-09-13 18:19:22摩羯座
日期:2014-10-10 17:43:02水瓶座
日期:2014-10-16 01:00:22
3 [报告]
发表于 2014-09-13 18:17 |只看该作者
本帖最后由 李满满 于 2014-09-13 18:18 编辑

学生党:飘过~
看来楼主对{}和正则的{}搞混淆了?
1.首先{}是通配符也是内部组也是高级变量组
-通配符用法touch {1,2,3}.txt,{}中以逗号分割不允许有空白除非\转义
-内部组用法{ command1;command2; },{}内部命令以;隔开,并且头尾都必须有空格
-高级变量用法${yhsafe:n:m} ${yhsafe%%\/} ${yhsafe#\/}等等吧

2.你认为的正则其实是这样的如果是基本正则的话就是\{\} 如果是扩展正则的话就是{}了,当然还要根据你调用的工具来进一步

论坛徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亚冠之浦和红钻
日期:2015-06-29 15:30:48
4 [报告]
发表于 2014-09-13 19:07 |只看该作者
  1.    Brace Expansion
  2.        Brace expansion is a mechanism by which arbitrary strings may be generated.  This mechanism is similar to  pathname  expansion,  but  the
  3.        filenames  generated need not exist.  Patterns to be brace expanded take the form of an optional preamble, followed by either a series of
  4.        comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript.  The preamble is  prefixed
  5.        to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.

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

  8.        A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional incre-
  9.        ment,  is  an  integer.  When integers are supplied, the expression expands to each number between x and y, inclusive.  Supplied integers
  10.        may be prefixed with 0 to force each term to have the same width.  When either x or y begins with a zero, the shell attempts to force all
  11.        generated terms to contain the same number of digits, zero-padding where necessary.  When characters are supplied, the expression expands
  12.        to each character lexicographically between x and y, inclusive.  Note that both x and y must be of the same type.  When the increment  is
  13.        supplied, it is used as the difference between each term.  The default increment is 1 or -1 as appropriate.

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

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

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

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

  24.        Brace  expansion  introduces  a  slight incompatibility with historical versions of sh.  sh does not treat opening or closing braces spe-
  25.        cially when they appear as part of a word, and preserves them in the output.  Bash removes braces from words as a  consequence  of  brace
  26.        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
  27.        after expansion by bash.  If strict compatibility with sh is desired, start bash with the +B option or disable brace expansion  with  the
  28.        +B option to the set command (see SHELL BUILTIN COMMANDS below).
复制代码
参见man bash

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
5 [报告]
发表于 2014-09-13 19:22 |只看该作者
回复 1# 聆雨淋夜

{} 有很多用法, 但{} 从来没有当作通配符使用。

你说的是花括号展开(brace expansion), 见 @dn833 的贴子。

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
6 [报告]
发表于 2014-09-13 19:50 |只看该作者
回复 3# 李满满

我这里也不是正则的用法,正则中{ }是修饰符,用于表示前一个字符出现的次数。


   

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
7 [报告]
发表于 2014-09-13 20:01 |只看该作者
回复 3# 李满满
你说的内部组用法和高级用法,是把它当shell元字符使用的,用于构建无名函数和截取字符串,我的疑惑是对它通配符的用法,你列举的通配符用法跟我的例子一样诶。

   

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
8 [报告]
发表于 2014-09-13 20:03 |只看该作者
回复 5# blackold

我也认为{ }没有通配符的用法,好多书籍都说这是通配符的用法,哈哈,误导不少人啊

   

论坛徽章:
3
丑牛
日期:2014-09-13 18:19:22摩羯座
日期:2014-10-10 17:43:02水瓶座
日期:2014-10-16 01:00:22
9 [报告]
发表于 2014-09-13 20:10 |只看该作者
说是通配/扩展作用~回复 8# 聆雨淋夜


   

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
10 [报告]
发表于 2014-09-13 20:14 |只看该作者
回复 9# 李满满
也可以看看blackold的回复


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP