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
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
- Linux Commands for Beginners-- Regular Expressions--the grep command
- Linux Commands for Beginners--The top Command
- Linux Commands for Beginners--The Watch Command
- Linux Commands for Beginners--The .bashrc File
- Linux Commands for Beginners--Changing Ownership --chown command
- Linux Commands for Beginners-- Changing Permissions --chmod command
- Linux Commands for Beginners--Head and Tail Command
- Linux Commands For Beginners: SUDO
- Linux Commands for Beginners--Finding Files
- Linux Commands for Beginners--Create Your Own Commands Via alias
- Linux Commands for Beginners--which and whatis commands
- Linux Commands for Beginners--Viewing Resources--du,df,free commands
- Linux Commands for Beginners--Arithmetic in Bash--expr,let commands
- Linux Commands for Beginners---SSH for Remote Administration
- Linux Commands for Beginners-- FTP for Remote Administration
- Linux Commands for Beginners-- Echo And Creating Variables
- Linux Commands for Beginners--Creating Your First Scripting
- Regular DLL Tutor For Beginners
- xen网络后端驱动分析(发送篇)
- iOS上的socket通信
- WIN7 旗舰版IIS的查看与安装
- Oracle数据库导入导出
- 黑马程序员_21 银行业务调度系统
- Linux Commands for Beginners-- Regular Expressions--the grep command
- adb服务启动失败处理命令
- 【python初学】TXT文档行数统计
- POJ 2109 Power of Cryptography 语言技巧
- 命令行运行java类找不到类异常
- git的使用
- hadoop配置文件
- ios两页面跳转实现
- boost assign 库