sweli 发表于 2018-8-25 07:33:05

shell下字符的匹配问题

  Shell环境下如何匹配tab也就是空白分隔符呢?
  在man bash中我们找到这么一段话
  Words of the form $'string' are treated specially.The word expands to
  string, with backslash-escaped characters replaced as specified bythe ANSICstandard.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 istheoctalvalue
  nnn (one to three digits)
  \xHH   theeight-bitcharacterwhose value is the hexadecimal
  value HH (one or two hex digits)
  \cx    a control-x character
  The expanded result is single-quoted, as if thedollarsignhadnot been present.
  也就是说在shell环境下可以使用\t的转义序列,需要在前面加上$符号,让bash本身分开来识别。

    问题:我们如何通过48和后面的一个tab来过滤出第一行呢?
  # cat temp
  48      Mike
  480   Ken
  48
  方法一:
  # grep $'48\t' temp
  48      Mike
  

  方法二:
  # grep '48' temp
  48      Mike
  方法三:
  # grep -P '48\t' temp
  48      Mike
  -P, --perl-regexp
  Interpret PATTERN as a Perl regular expression.
  方法四:
  # grep '48[[:space:]]' temp
  48      Mike
  方法五:
  # sed -n '/48\t/p' temp
  48      Mike
  方法六:
  # awk '/48\t/' temp
  48      Mike

页: [1]
查看完整版本: shell下字符的匹配问题