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 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.
 

也就是说在shell环境下可以使用\t的转义序列,需要在前面加上$符号,让bash本身分开来识别。

 

问题:我们如何通过48和后面的一个tab来过滤出第一行呢?

[root@managevm1 ~]# cat temp
48      Mike
480     Ken
48

方法一:     
[root@managevm1 ~]# grep $'48\t' temp
48      Mike

方法二:

[root@managevm1 ~]# grep '48<按ctrl+v在按tab>' temp
48      Mike

方法三:   

[root@managevm1 ~]# grep -P '48\t' temp
48      Mike

       -P, --perl-regexp
              Interpret PATTERN as a Perl regular expression.

方法四:

[root@managevm1 ~]# grep '48[[:space:]]' temp
48      Mike

方法五:

[root@managevm1 ~]# sed -n '/48\t/p' temp
48      Mike

方法六:

[root@managevm1 ~]# awk '/48\t/' temp
48      Mike

 


 

你可能感兴趣的:(shell,职场,tab,过滤,休闲)