免费注册 查看新帖 |

Chinaunix

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

[文本处理] 请问这段代码中的“\177”中的“\”起什么作用? [复制链接]

论坛徽章:
6
摩羯座
日期:2013-12-27 09:45:04技术图书徽章
日期:2014-01-27 12:40:06辰龙
日期:2014-02-28 15:12:52巳蛇
日期:2014-03-21 17:06:27未羊
日期:2014-04-15 20:12:41黑曼巴
日期:2016-08-02 11:00:06
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-08-02 09:22 |只看该作者 |倒序浏览
  1.     {
  2.         lineLength=length($0)
  3.         for (i=1; i<=lineLength; ++i){
  4.             if (substr($0, i, 1) > "\177")
  5.             {
  6.                 print FILENAME,FNR,$0
  7.             }
  8.         }
  9.     }
复制代码

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2016-08-02 10:30 |只看该作者
回复 1# laliheyi

GAWK manual

3.2 Escape Sequences

Some characters cannot be included literally in string constants ("foo") or regexp constants (/foo/). Instead, they should be represented with escape sequences, which are character sequences beginning with a backslash (‘\’). One use of an escape sequence is to include a double-quote character in a string constant. Because a plain double quote ends the string, you must use ‘\"’ to represent an actual double-quote character as a part of the string. For example:

$ awk 'BEGIN { print "He said \"hi!\" to her." }'
-| He said "hi!" to her.

The backslash character itself is another character that cannot be included normally; you must write ‘\\’ to put one backslash in the string or regexp. Thus, the string whose contents are the two characters ‘"’ and ‘\’ must be written "\"\\".

Other escape sequences represent unprintable characters such as TAB or newline. There is nothing to stop you from entering most unprintable characters directly in a string constant or regexp constant, but they may look ugly.

The following list presents all the escape sequences used in awk and what they represent. Unless noted otherwise, all these escape sequences apply to both string constants and regexp constants:

\\    A literal backslash, ‘\’.
\a    The “alert” character, Ctrl-g, ASCII code 7 (BEL). (This often makes some sort of audible noise.)
\b    Backspace, Ctrl-h, ASCII code 8 (BS).
\f    Formfeed, Ctrl-l, ASCII code 12 (FF).
\n    Newline, Ctrl-j, ASCII code 10 (LF).
\r    Carriage return, Ctrl-m, ASCII code 13 (CR).
\t    Horizontal TAB, Ctrl-i, ASCII code 9 (HT).
\v    Vertical TAB, Ctrl-k, ASCII code 11 (VT).
\nnn    The octal value nnn, where nnn stands for 1 to 3 digits between ‘0’ and ‘7’. For example, the code for the ASCII ESC (escape) character is ‘\033’.
\xhh…    The hexadecimal value hh, where hh stands for a sequence of hexadecimal digits (‘0’–‘9’, and either ‘A’–‘F’ or ‘a’–‘f’). Like the same construct in ISO C, the escape sequence continues until the first nonhexadecimal digit is seen. (c.e.) However, using more than two hexadecimal digits produces undefined results. (The ‘\x’ escape sequence is not allowed in POSIX awk.)

        CAUTION: The next major release of gawk will change, such that a maximum of two hexadecimal digits following the ‘\x’ will be used.

\/    A literal slash (necessary for regexp constants only). This sequence is used when you want to write a regexp constant that contains a slash (such as /.*:\/home\/[[:alnum:]]+:.*/; the ‘[[:alnum:]]’ notation is discussed in Bracket Expressions). Because the regexp is delimited by slashes, you need to escape any slash that is part of the pattern, in order to tell awk to keep processing the rest of the regexp.
\"    A literal double quote (necessary for string constants only). This sequence is used when you want to write a string constant that contains a double quote (such as "He said \"hi!\" to her."). Because the string is delimited by double quotes, you need to escape any quote that is part of the string, in order to tell awk to keep processing the rest of the string.

In gawk, a number of additional two-character sequences that begin with a backslash have special meaning in regexps. See GNU Regexp Operators.
   

论坛徽章:
6
摩羯座
日期:2013-12-27 09:45:04技术图书徽章
日期:2014-01-27 12:40:06辰龙
日期:2014-02-28 15:12:52巳蛇
日期:2014-03-21 17:06:27未羊
日期:2014-04-15 20:12:41黑曼巴
日期:2016-08-02 11:00:06
3 [报告]
发表于 2016-08-02 11:00 |只看该作者
回复 2# jason680


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP