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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值