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