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

grep is a command-line utility for searching plain-text data for lines which match 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.

 

Here I have considered rootadminz.txt is the base file where we will search pattern with the help of grep command in this article for an 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-Z, a-z & 0-9 (Alphanumeric Characters).

$ grep "^[[:alnum:]]" rootadminz.txt

 

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:]]" rootadminz.txt

 

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:]]" rootadminz.txt

 

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:]]" rootadminz.txt

 

5. Search Lower Letters

 

Another option for grep is to search line which will start from lower letters i.e [a-z] (Lower Letters).

$ grep "^[[:lower:]]" rootadminz.txt

 

6. Search Punctuation Characters

 

The Punctuation characters for grep is to search line which will start from [! ” # $ % & ‘ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~. ] i.e. Punctuation Characters.

$ grep "^[[:punct:]]" rootadminz.txt

 

7. Search Graphical Characters

 

The grep is also used to search a line which will start from Alphanumeric & Punctuation Characters called as Graphical Characters.

$ grep "^[[:graph:]]" rootadminz.txt

 

8. Search Printable Characters

 

Similarly like Graphical Characters, grep is useful to search a line which will start from Alphanumeric, Punctuation and space characters.

$ grep "^[[:print:]]" rootadminz.txt

 

9. Search Space Characters

 

The grep has also a functionality to search a line which will start from [tab, newline, vertical tab, form feed, carriage return, and space] i.e. Space Characters.

$ grep "^[[:space:]]" rootadminz.txt

 

10. Search Uppercase Letters

 

Another option in the grep is also used to search a line which will start from [A-Z] i.e Upper-case Letters.

$ grep "^[[:upper:]]" rootadminz.txt

 

11. Search Hexadecimal Digits

 

The grep searches a line which will start from [0-9, A-F and a-f] i.e Hexadecimal Digits.

$ grep "^[[:xdigit:]]" rootadminz.txt

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)