Lesson 1.3: Use grep and regular expressions to analyze text
The grep command is one of the most useful commands in a Linux terminal environment. The name grep stands for “global regular expression print”. This means that you can use grep to check whether the input it receives matches a specified pattern.
- [ ]: Matches any one of a set characters
- with hyphen: Matches any one of a range characters
- ^: The pattern following it must occur at the beginning of each line
- ^ with [ ] : The pattern must not contain any character in the set specified
- $: The pattern preceding it must occur at the end of each line
- . (dot): Matches any one character
- \ (backslash): Ignores the special meaning of the character following it
- *: zero or more occurrences of the previous character
- (dot).*: Nothing or any numbers of characters.
Example File:
[sanjeeb@client ~]$ cat example_file Newa Newb Newc Newd Newe Newf Newg Newh
Example 1: [ ] : Matches any one of a set characters
[sanjeeb@client ~]$ grep "New[a,b,c]" example_file Newa Newb Newc
[sanjeeb@client ~]$ grep "New[h][d]" example_file Newhdef [sanjeeb@client ~]$ grep "New[h][def]" example_file Newhdef [sanjeeb@client ~]$ grep "New[h][defghi]" example_file Newhdef Newhghi
Example 2: Use [ ] with hyphen: Matches any one of a range characters
[sanjeeb@client ~]$ grep "New[0-1][d-o]" example_file New0def New0ghi New0jkl New0mno New1def New1ghi New1jkl New1mno
Example 3: Use ^: The pattern following it must occur at the beginning of each line
[sanjeeb@client ~]$ grep "^old" example_file old1file old2image old3audio old4video old5readme [sanjeeb@serverB test]$ cat testfile new1 new2 new3 new4 new5 new6 new7 new8 new9 This is new file1 This is new file2 # This is comment 1 # This is comment 2 [sanjeeb@serverB test]$ grep "^[#]" testfile # This is comment 1 # This is comment 2 [sanjeeb@serverB test]$ grep "^[^#]" testfile new1 new2 new3 new4 new5 new6 new7 new8 new9 This is new file1 This is new file2
Example 4: Use ^ with [ ]: The pattern must not contain any character in the set specified
[sanjeeb@client ~]$ grep "^[a-d]" example_file a boy bad boy cute puppy dead plant
Example 5: Use $: The pattern preceding it must occur at the end of each line
[sanjeeb@client ~]$ grep "y$" example_file a boy bad boy cute puppy jersey lonely money
[sanjeeb@client ~]$ grep "oy$" example_file a boy bad boy
Example 6: Use . (dot): Matches any one character
[sanjeeb@client ~]$ grep "b.y$" example_file a boy bad boy bengal bay
Example 7: Use \ (backslash): Ignores the special meaning of the character following it
[sanjeeb@client ~]$ grep "New.pqr" example_file Newhpqr New2pqr New.pqr
We can see that the special character is not ignored so prints both h and 2
[sanjeeb@client ~]$ grep "New\.\pqr" example_file New.pqr
By adding Backslash
Example 8: Use *: zero or more occurrences of the previous character
[sanjeeb@client ~]$ grep "kni*" example_file knife [sanjeeb@client ~]$ grep "kni*f*" example_file knife
Example 9: Use (dot).*: Nothing or any numbers of characters.
[sanjeeb@client ~]$ grep ".*abc" example_file Newhabc New0abc New1abc New$abc [sanjeeb@client ~]$ grep ".*boy" example_file a boy bad boy