天天看點

Linux Commands for Beginners-- Regular Expressions--the grep command

 In this part,I wil show you the basis of  Regular Expressions and the grep command

1.Command:

     grep

  DESCRIPTION:

     print lines matching a pattern

  SYNOPSIS:

     grep [OPTIONS] PATTERN [FILE...]

  OPTION:

    -n, --line-number         print line number with output lines

Regular expressions made up of anchors,character sets,modifiers

Anchors:specify the position

^ :at the beginnig of a line

$ :at the end of a line

Tips:If ^ is not placed at the beginning ,or $ at the end,the two won't act as anchors anymore

character sets:what is seached

Example:

[email protected]:~/tutorial# ls

A Girl.txt

[email protected]:~/tutorial# cat -n A\ Girl.txt

     1    the tree has entered my hands

     2    the sap has ascended my arms

     3    the tree has grown in my breast-

     4    downward

     5    the branches grow out of me, like arms

     6    

     7    tree you are

     8    moss you are

     9    you are violets with wind above them

    10    a child - so high - you are

    11    and all this is folly to the world

print lines contain words "tree"   

[email protected]:~/tutorial# grep 'tree' A\ Girl.txt

the tree has entered my hands

the tree has grown in my breast-

tree you are

print lines contain word "tree" and print which line they are in

[email protected]:~/tutorial# grep -n 'tree' A\ Girl.txt

1:the tree has entered my hands

3:the tree has grown in my breast-

7:tree you are

print lines start with word "tree" and print which line they are in

[email protected]:~/tutorial# grep -n ^'tree' A\ Girl.txt

7:tree you are

print lines contain word "are" and print which line they are in

[email protected]:~/tutorial# grep -n 'are' A\ Girl.txt

7:tree you are

8:moss you are

9:you are violets with wind above them

10:a child - so high - you are

print lines end with word "are" and print which line they are in

[email protected]:~/tutorial# grep -n 'are'$ A\ Girl.txt

7:tree you are

8:moss you are

10:a child - so high - you are

Matching Character Sets

--"abc" finds lines with "abc" in them

--match any character with "."(dot)

--specify a range with []

  [123] - lines that contain 1,2 or 3

  [0-9] - lines that contain at least a number

  [A-Za-z0-9] - lines that contain at leas a letters or a numbers

Example:

Find words start with letter 'm' and follow with a any character

[email protected]:~/tutorial# grep -n 'm.' A\ Girl.txt

1:the tree has entered my hands

2:the sap has ascended my arms

3:the tree has grown in my breast-

5:the branches grow out of me, like arms

8:moss you are

Find words start with letter 'a' or 'b'

[email protected]:~/tutorial# grep -n ^'[ad]' A\ Girl.txt

4:downward

10:a child - so high - you are

11:and all this is folly to the world

print lines start with a letter range from 'a' to 'm'

[email protected]:~/tutorial# grep -n ^'[a-m]' A\ Girl.txt

4:downward

8:moss you are

10:a child - so high - you are

11:and all this is folly to the world

Tips:

If you want to search for "[" or "]",you should use backslash "\"

Example:

[\]] - search lines with "]" in them

modifiers:specify how many times the previous charater set is repeated

* - matches zero or more copies of the chars

[]\{1,4\} - lines that have between 1 to 4 mathes

\<word\> - searches for a specific and separate word

Example:

[email protected]:~/tutorial# ls

A Girl.txt  essay.txt

[email protected]:~/tutorial# cat -n essay.txt

     1    a

     2    aa

     3    aaa

     4    aaaa

     5    aaaaa

     6    aaaaaa

     7    

     8    abc

     9    abbc

    10    abcc

print lines contain word that mach [b-c][b-c]

[email protected]:~/tutorial# grep -n '[b-c]\{2,3\}' essay.txt

8:abc

9:abbc

10:abcc

If you want to just search specify word,following command failed to work.

[email protected]:~/tutorial# grep -n 'aa' essay.txt

2:aa

3:aaa

4:aaaa

5:aaaaa

6:aaaaaa

therefore,you can type command like this

[email protected]:~/tutorial# grep -n '\<aa\>' essay.txt

2:aa

繼續閱讀