免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: lzfzl
打印 上一主题 下一主题

sed如何替换特殊字符:单引号、大于号、等号? [复制链接]

论坛徽章:
0
21 [报告]
发表于 2007-12-14 16:28 |只看该作者
下面是 bash man 中关于 Quoting 一节,

可以解释上面的问题

QUOTING
       Quoting is used to remove the special meaning of certain characters or
       words  to the shell.  Quoting can be used to disable special treatment
       for special characters, to prevent reserved words  from        being  recog-
       nized as such, and to prevent parameter expansion.

       Each  of the metacharacters listed above under DEFINITIONS has special
       meaning to the shell and must be quoted if it is to represent  itself.

       When the command history expansion facilities are being used, the his-
       tory expansion character, usually !, must be quoted to prevent history
       expansion.

       There  are  three  quoting  mechanisms:        the  escape character, single
       quotes, and double quotes.

       A non-quoted backslash (\) is the escape character.  It preserves  the
       literal        value  of the next character that follows, with the exception
       of <newline>.  If a \<newline> pair appears, and the backslash is  not
       itself  quoted, the \<newline> is treated as a line continuation (that
       is, it is removed from the input stream and effectively ignored).

       Enclosing characters in single quotes preserves the literal  value  of
       each  character        within        the  quotes.   A  single  quote may not occur
       between single quotes, even when preceded by a backslash.

       Enclosing characters in double quotes preserves the literal  value  of
       all  characters        within the quotes, with the exception of $, ‘, and \.
       The characters $ and ‘ retain  their  special  meaning  within  double
       quotes.The backslash retains its special meaning only when followed
       by one of the following characters: $, ‘, ", \, or <newline>.  
A dou-
       ble  quote  may        be quoted within double quotes by preceding it with a
       backslash.  When command history is being used, the double  quote  may
       not be used to quote the history expansion character.


       The  special  parameters         *  and @ have special meaning when in double
       quotes (see PARAMETERS below).

       Words of the form $’string’ are treated specially.  The        word  expands
       to  string,  with backslash-escaped characters replaced as specifed by
       the ANSI C standard.  Backslash        escape        sequences,  if        present,  are
       decoded as follows:
              \a     alert (bell)
              \b     backspace
              \e     an escape character
              \f     form feed
              \n     new line
              \r     carriage return
              \t     horizontal tab
              \v     vertical tab
              \\     backslash
              \’     single quote
              \nnn   the  eight-bit  character whose value is the octal value
                     nnn (one to three digits)
              \xHH   the eight-bit character whose value is  the  hexadecimal
                     value HH (one or two hex digits)
              \cx    a control-x character

       The  expanded  result  is single-quoted, as if the dollar sign had not
       been present.

       A double-quoted string preceded by a dollar sign ($)  will  cause  the
       string  to be translated according to the current locale.  If the cur-
       rent locale is C or POSIX, the dollar sign is ignored.  If the  string
       is translated and replaced, the replacement is double-quoted.

[ 本帖最后由 ailms 于 2007-12-14 18:20 编辑 ]

论坛徽章:
0
22 [报告]
发表于 2007-12-14 16:32 |只看该作者
"\\\v" 和 \\\v 的区别就是 :

前者的第1个 \ 具备 escape 功能,而第3个\由于处于 " " 中,后面不是上面红色字体提到的那4个字符($、,、"、<newline>),

所以不具备 escape 功能,也就是这个 \ 是普通字符,所以就扩展为 "\\v" 了

后者由于没有被括起来,所以第1和第3个 \ 都具备 quoting 的功能,也就是变成 \v 了

[ 本帖最后由 ailms 于 2007-12-14 16:33 编辑 ]

评分

参与人数 1可用积分 +9 收起 理由
sakulagi + 9 我很赞同

查看全部评分

论坛徽章:
3
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:19
23 [报告]
发表于 2007-12-17 10:57 |只看该作者
学习了..感谢ailms兄..这两天有事..没上网.现在才回复.不好意思啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP