Pages

Thursday, June 29, 2017

11 Advanced Linux ‘Grep’ Commands on Character Classes and Bracket Expressions

Have you ever been into a situation where you need to search for a string, word or pattern inside a file? if yes, then the grep utility comes handy in such situation.
grep is a command line utility for searching plain-text data for lines which matching a regular expression. If you will divide the word grep like g/re/p then the meaning of grep is (globally search a regular expression and print) which search pattern from the file and print the line on the screen i.e. standard output.
In this article I will be going to explain advanced commands on grep for the Character Classes in Linux and Unix like operating system.
Here I have considered tecmint.txt is the base file where we will search pattern with the help of grep command in this article for explanation.

1. Search Alphanumeric Characters

If you have thousands of lines in a file and wanted to search a line which will start from only A-Za-z & 0-9(Alphanumeric Characters).
$ grep "^[[:alnum:]]" tecmint.txt
Grep - Search Alphanumeric Characters in File
Grep – Search Alphanumeric Characters in File

2. Search Alpha Characters

Similar options like if you want to search line which will start from only [A-Z & a-z] i.e. Alpha Characters.
$ grep "^[[:alpha:]]" tecmint.txt
Grep - Search Alpha Characters in File
Grep – Search Alpha Characters in File

3. Search Blank Characters

Another options like if you want to search line which will start from [Tab & Space] i.e. Blank Characters.
$ grep "^[[:blank:]]" tecmint.txt
Grep - Search for Spaces or Tabs in File
Grep – Search for Spaces or Tabs in File

4. Search Digit Characters

The digit option for grep is also very useful to search line which will start from digit [0-9] i.e. Digit Characters.
$ grep "^[[:digit:]]" tecmint.txt
Grep - Search Number Characters in File
Grep – Search Number Characters in File

5. Search Lower Letters

Another option for grep is to search line which will start from lower letters i.e [a-z] (Lower Letters).
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
                                                    ► Read more: http://adf.ly/1nBihF
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

No comments:

Post a Comment