grep and regular expression --- ^ . *

"^" : Start of Line anchor
"." : Matches any single character except the newline character.
"*" : Matches the preceding character 0 or more times.

Application_1

test file

if_0        // no white space, no tab space
        if_1        // a tab space
  if_2        // two white space

command :

$ grep "^if" test
if_0

Conclusion :
^ is used to match the first column string

Application_2

test file

if_0 CROSS_COMPILE
	if_1 CROSS_COMPILE
  if_2 CROSS_COMPILE

command_1 :

$ grep -rns "^if.*CROSS" test
1:if_0 CROSS_COMPILE

command_2 :

$ grep -rns ".*if.*CROSS" test
1:if_0 CROSS_COMPILE
3:	if_1 CROSS_COMPILE
4:  if_2 CROSS_COMPILE

Conclusion :
For coding style, programmer place white or tab space in front of if.
If you want to find if as start, have to use ".*"

Application_3

test file

aaa1
aaa1
bbb2
bbb2
ccc3
ccc3

command_1 :

$ grep "^bbb" test
bbb2
bbb2

command_2 :

$ grep "^[^bbb]" test
aaa1
aaa1
ccc3
ccc3
原文地址:https://www.cnblogs.com/youchihwang/p/9741749.html