- let's view the content of the file we are going to work with
[root@RHEL5-Serv01 work]# cat grepfile2
first line
second line
third line
fourth line
fifth line
seventh line
eighth line
nineth line
tenth line
ten lines in total contain the word line
first line
second line
third line
fourth line
fifth line
seventh line
eighth line
nineth line
tenth line
ten lines in total contain the word line
- let's grep the word "ten"
[root@RHEL5-Serv01 work]# grep ten grepfile2
tenth line
ten lines in total containing the word line
tenth line
ten lines in total containing the word line
- let's count the number of match
[root@RHEL5-Serv01 work]# grep -c ten grepfile2
2
2
- ignoring case of the word
[root@RHEL5-Serv01 work]# grep TEN grepfile2
[root@RHEL5-Serv01 work]#
※ NO MATCH BUT WHEN IGNORING CASE AS FOLLOWS
[root@RHEL5-Serv01 work]# grep -i TEN grepfile2
tenth line
ten lines in total containing the word line
[root@RHEL5-Serv01 work]#
※ NO MATCH BUT WHEN IGNORING CASE AS FOLLOWS
[root@RHEL5-Serv01 work]# grep -i TEN grepfile2
tenth line
ten lines in total containing the word line
- lets reduce the above result to an exact match
[root@RHEL5-Serv01 work]# grep -w ten grepfile2
ten lines in total containing the word line
ten lines in total containing the word line
- matching multiple words
[root@RHEL5-Serv01 work]# grep -e "first" -e "second" grepfile2
first line
second line
※ the following gives the same result as the command above
[root@RHEL5-Serv01 work]# grep -E 'first|second' grepfile2
first line
second line
first line
second line
※ the following gives the same result as the command above
[root@RHEL5-Serv01 work]# grep -E 'first|second' grepfile2
first line
second line
- matching two lines after the matching word
[root@RHEL5-Serv01 work]# grep -A 2 first grepfile2
first line
second line
third line
first line
second line
third line
- matching two lines before the matching word
[root@RHEL5-Serv01 work]# grep -B 2 third grepfile2
first line
second line
third line
first line
second line
third line
- matching two lines before and after the matching word
[root@RHEL5-Serv01 work]# grep -C 2 third grepfile2
first line
second line
third line
fourth line
fifth line
first line
second line
third line
fourth line
fifth line
- showing lines that do not match the search word
[root@RHEL5-Serv01 work]# grep -v line grepfile2
[root@RHEL5-Serv01 work]#
- let's find the file containing our search word
[root@RHEL5-Serv01 work]# ls
grepfile grepfile1 grepfile2
[root@RHEL5-Serv01 work]# grep -l line grepfile*
grepfile2
grepfile grepfile1 grepfile2
[root@RHEL5-Serv01 work]# grep -l line grepfile*
grepfile2
- let's find the line number that containts the search word
[root@RHEL5-Serv01 work]# grep -n line grepfile2
1:first line
2:second line
3:third line
4:fourth line
5:fifth line
6:seventh line
7:eighth line
8:nineth line
9:tenth line
10:ten lines in total contain the word line
GREP USING REGULAR EXPRESSIONS
$ cat file
i am
you are
he is
she is
we are
you are
they are
so what do we do?!
080-9876-9999
ab2354cF
$ grep 'a.e' file
you are
we are
you are
they are
$ grep 'w[a-z]\{2\}t' file(same as "grep w..t file")
so what do we do?!
$grep '[0-9]\{3\}-[0-9]\{4\}-[0-9]\{4\}' file
080-9876-9999
No comments:
Post a Comment