Chinaunix

标题: 请问这段代码中的“\177”中的“\”起什么作用? [打印本页]

作者: laliheyi    时间: 2016-08-02 09:22
标题: 请问这段代码中的“\177”中的“\”起什么作用?
  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.     }
复制代码

作者: jason680    时间: 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.
   
作者: laliheyi    时间: 2016-08-02 11:00
回复 2# jason680


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2