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

来源:互联网 发布:mac word打开pdf 编辑:程序博客网 时间:2024/05/17 23:12
 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:
root@piniheaven:~/tutorial# ls
A Girl.txt
root@piniheaven:~/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"   
root@piniheaven:~/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
root@piniheaven:~/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
root@piniheaven:~/tutorial# grep -n ^'tree' A\ Girl.txt
7:tree you are


print lines contain word "are" and print which line they are in
root@piniheaven:~/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
root@piniheaven:~/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
root@piniheaven:~/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'
root@piniheaven:~/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'
root@piniheaven:~/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:

root@piniheaven:~/tutorial# ls
A Girl.txt  essay.txt
root@piniheaven:~/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]
root@piniheaven:~/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.
root@piniheaven:~/tutorial# grep -n 'aa' essay.txt
2:aa
3:aaa
4:aaaa
5:aaaaa
6:aaaaaa

therefore,you can type command like this
root@piniheaven:~/tutorial# grep -n '\<aa\>' essay.txt
2:aa






原创粉丝点击