{"id":13277,"date":"2019-04-01T11:05:57","date_gmt":"2019-04-01T11:05:57","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13277"},"modified":"2019-04-01T11:05:57","modified_gmt":"2019-04-01T11:05:57","slug":"11-advanced-linux-grep-commands-on-character-classes-and-bracket-expressions","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/11-advanced-linux-grep-commands-on-character-classes-and-bracket-expressions\/","title":{"rendered":"11 Advanced Linux \u2018Grep\u2019 Commands on Character Classes and Bracket Expressions"},"content":{"rendered":"<p>Have you ever been into a situation where you need to\u00a0<a href=\"https:\/\/www.tecmint.com\/awk-filter-text-or-string-using-patterns\/\" target=\"_blank\" rel=\"noopener\">search for a string, word or pattern<\/a>\u00a0inside a file? if yes, then the\u00a0<strong>grep<\/strong>\u00a0utility comes handy in such situation.<\/p>\n<p><strong>grep<\/strong>\u00a0is a\u00a0command line utility for searching plain-text data for lines which matching a\u00a0regular expression. If you will divide the word\u00a0<strong>grep<\/strong>\u00a0like\u00a0<strong>g\/re\/p<\/strong>\u00a0then the meaning of\u00a0<strong>grep<\/strong>\u00a0is (globally search a\u00a0regular\u00a0expression\u00a0and\u00a0print) which search pattern from the file and print the line on the screen i.e. standard output.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/12-practical-examples-of-linux-grep-command\/\" target=\"_blank\" rel=\"noopener\">12 Basic Practical Examples of Linux grep Command<\/a><\/p>\n<p>In this article I will be going to explain advanced commands on\u00a0<strong>grep<\/strong>\u00a0for the\u00a0<strong>Character Classes<\/strong>\u00a0in Linux and Unix like operating system.<\/p>\n<p>Here I have considered\u00a0<strong>tecmint.txt<\/strong>\u00a0is the base file where we will search pattern with the help of\u00a0<strong>grep<\/strong>\u00a0command in this article for explanation.<\/p>\n<h3>1. Search Alphanumeric Characters<\/h3>\n<p>If you have thousands of lines in a file and wanted to search a line which will start from only\u00a0<code>A-Z<\/code>,\u00a0<code>a-z<\/code>\u00a0&amp;\u00a0<code>0-9<\/code>(<strong>Alphanumeric Characters<\/strong>).<\/p>\n<pre>$ grep \"^[[:alnum:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22460\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Alphanumeric-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22460\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Alphanumeric-Characters-in-Linux.png\" alt=\"Grep - Search Alphanumeric Characters in File\" width=\"589\" height=\"225\" aria-describedby=\"caption-attachment-22460\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22460\" class=\"wp-caption-text\">Grep \u2013 Search Alphanumeric Characters in File<\/p>\n<\/div>\n<h3>2. Search Alpha Characters<\/h3>\n<p>Similar options like if you want to search line which will start from only\u00a0<code>[A-Z &amp; a-z]<\/code>\u00a0i.e.\u00a0<code>Alpha Characters<\/code>.<\/p>\n<pre>$ grep \"^[[:alpha:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22461\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Alpha-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22461\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Alpha-Characters-in-Linux.png\" alt=\"Grep - Search Alpha Characters in File\" width=\"599\" height=\"170\" aria-describedby=\"caption-attachment-22461\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22461\" class=\"wp-caption-text\">Grep \u2013 Search Alpha Characters in File<\/p>\n<\/div>\n<h3>3. Search Blank Characters<\/h3>\n<p>Another options like if you want to search line which will start from\u00a0<code>[Tab &amp; Space]<\/code>\u00a0i.e.\u00a0<strong>Blank Characters<\/strong>.<\/p>\n<pre>$ grep \"^[[:blank:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22462\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Blank-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22462\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Blank-Characters-in-Linux.png\" alt=\"Grep - Search for Spaces or Tabs in File\" width=\"603\" height=\"44\" aria-describedby=\"caption-attachment-22462\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22462\" class=\"wp-caption-text\">Grep \u2013 Search for Spaces or Tabs in File<\/p>\n<\/div>\n<h3>4. Search Digit Characters<\/h3>\n<p>The digit option for grep is also very useful to search line which will start from digit\u00a0<code>[0-9]<\/code>\u00a0i.e.\u00a0<strong>Digit Characters<\/strong>.<\/p>\n<pre>$ grep \"^[[:digit:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22463\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Digit-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22463\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Digit-Characters-in-Linux.png\" alt=\"Grep - Search Number Characters in File\" width=\"600\" height=\"57\" aria-describedby=\"caption-attachment-22463\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22463\" class=\"wp-caption-text\">Grep \u2013 Search Number Characters in File<\/p>\n<\/div>\n<h3>5. Search Lower Letters<\/h3>\n<p>Another option for grep is to search line which will start from lower letters i.e\u00a0<code>[a-z]<\/code>\u00a0(<strong>Lower Letters<\/strong>).<\/p>\n<pre>$ grep \"^[[:lower:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22464\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Lower-Words-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22464\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Lower-Words-in-Linux.png\" alt=\"Grep - Search Lower Letters or Words in File\" width=\"602\" height=\"148\" aria-describedby=\"caption-attachment-22464\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22464\" class=\"wp-caption-text\">Grep \u2013 Search Lower Letters or Words in File<\/p>\n<\/div>\n<h3>6. Search Punctuation Characters<\/h3>\n<p>The Punctuation characters for grep is to search line which will start from\u00a0<strong>[! \u201d # $ % &amp; \u2018 ( ) * + , \u2013 . \/ : ; &lt; = &gt; ? @ [ \\ ] ^ _ ` { | } ~. ]<\/strong>\u00a0i.e.\u00a0<strong>Punctuation Characters<\/strong>.<\/p>\n<pre>$ grep \"^[[:punct:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22465\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Punctuation-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22465\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Punctuation-Characters-in-Linux.png\" alt=\"Grep - Search Punctuation Characters in File\" width=\"602\" height=\"111\" aria-describedby=\"caption-attachment-22465\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22465\" class=\"wp-caption-text\">Grep \u2013 Search Punctuation Characters in File<\/p>\n<\/div>\n<h3>7. Search Graphical Characters<\/h3>\n<p>The grep is also used to search a line which will start from\u00a0<strong>Alphanumeric &amp; Punctuation Characters<\/strong>\u00a0called as\u00a0<strong>Graphical Characters<\/strong>.<\/p>\n<pre>$ grep \"^[[:graph:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22466\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Graphical-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22466\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Graphical-Characters-in-Linux.png\" alt=\"Grep - Search Graphical Characters in File\" width=\"600\" height=\"302\" aria-describedby=\"caption-attachment-22466\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22466\" class=\"wp-caption-text\">Grep \u2013 Search Graphical Characters in File<\/p>\n<\/div>\n<h3>8. Search Printable Characters<\/h3>\n<p>Similarly like\u00a0<strong>Graphical Characters<\/strong>, grep is useful to search a line which will start from Alphanumeric, Punctuation and space characters.<\/p>\n<pre>$ grep \"^[[:print:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22467\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Printable-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22467\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Printable-Characters-in-Linux.png\" alt=\"Grep - Search Printable Characters in File\" width=\"601\" height=\"313\" aria-describedby=\"caption-attachment-22467\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22467\" class=\"wp-caption-text\">Grep \u2013 Search Printable Characters in File<\/p>\n<\/div>\n<h3>9. Search Space Characters<\/h3>\n<p>The grep has also a functionality to search a line which will start from\u00a0<strong>[tab, newline, vertical tab, form feed, carriage return, and space]<\/strong>\u00a0i.e.\u00a0<strong>Space Characters<\/strong>.<\/p>\n<pre>$ grep \"^[[:space:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22468\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Space-Characters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22468\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Space-Characters-in-Linux.png\" alt=\"Grep - Search Space Characters in File\" width=\"604\" height=\"45\" aria-describedby=\"caption-attachment-22468\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22468\" class=\"wp-caption-text\">Grep \u2013 Search Space Characters in File<\/p>\n<\/div>\n<h3>10. Search Uppercase Letters<\/h3>\n<p>Another option in the grep is also used to search a line which will start from\u00a0<code>[A-Z]<\/code>\u00a0i.e\u00a0<strong>Upper-case Letters<\/strong>.<\/p>\n<pre>$ grep \"^[[:upper:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22469\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Uppercase-Letters-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22469\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Uppercase-Letters-in-Linux.png\" alt=\"Grep - Search Uppercase Letters in File\" width=\"602\" height=\"44\" aria-describedby=\"caption-attachment-22469\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22469\" class=\"wp-caption-text\">Grep \u2013 Search Uppercase Letters in File<\/p>\n<\/div>\n<h3>11. Search Hexadecimal Digits<\/h3>\n<p>The grep searches a line which will start from\u00a0<code>[0-9, A-F and a-f]<\/code>\u00a0i.e\u00a0<strong>Hexadecimal Digits<\/strong>.<\/p>\n<pre>$ grep \"^[[:xdigit:]]\" tecmint.txt\r\n<\/pre>\n<div id=\"attachment_22470\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Hexadecimal-Digits-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22470\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/08\/Search-Hexadecimal-Digits-in-Linux.png\" alt=\"Grep - Search Hexadecimal Digits in File\" width=\"601\" height=\"120\" aria-describedby=\"caption-attachment-22470\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-22470\" class=\"wp-caption-text\">Grep \u2013 Search Hexadecimal Digits in File<\/p>\n<\/div>\n<p>I have explained the advanced functionality of\u00a0<strong>grep<\/strong>\u00a0which is very strong and powerful tool to search the pattern in a File. Grep is also an important tool for shell scripting and programmers to search the pattern in the programs. It is worth to be familiar with other options and syntax to save the time.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/difference-between-grep-egrep-and-fgrep-in-linux\/\" target=\"_blank\" rel=\"noopener\">What\u2019s Difference Between Grep, Egrep and Fgrep in Linux?<\/a><\/p>\n<p>In case any issues on the commands which is explained in the article, you can post your comment in the comment section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-grep-commands-character-classes-bracket-expressions\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever been into a situation where you need to\u00a0search for a string, word or pattern\u00a0inside a file? if yes, then the\u00a0grep\u00a0utility comes handy in such situation. grep\u00a0is a\u00a0command line utility for searching plain-text data for lines which matching a\u00a0regular expression. If you will divide the word\u00a0grep\u00a0like\u00a0g\/re\/p\u00a0then the meaning of\u00a0grep\u00a0is (globally search a\u00a0regular\u00a0expression\u00a0and\u00a0print) which &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/11-advanced-linux-grep-commands-on-character-classes-and-bracket-expressions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;11 Advanced Linux \u2018Grep\u2019 Commands on Character Classes and Bracket Expressions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13277","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13277"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13277\/revisions"}],"predecessor-version":[{"id":13278,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13277\/revisions\/13278"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}