{"id":3649,"date":"2017-04-03T13:00:55","date_gmt":"2017-04-03T07:30:55","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3649"},"modified":"2017-04-03T13:00:55","modified_gmt":"2017-04-03T07:30:55","slug":"linux-regular-expressions","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/linux-regular-expressions\/","title":{"rendered":"LINUX &#8211; Regular Expresssions"},"content":{"rendered":"<h4 id=\"regular-expressions-in-linux-explained-with-examples\"><span style=\"color: #ff6600;\">Regular Expressions in Linux Explained with Examples<\/span><\/h4>\n<p>Regular expressions (Regexp) is one of the advanced concept we require to write efficient shell scripts and for effective system administration.<\/p>\n<ul>\n<li>Basically regular expressions are divided in to 3 types for better understanding.<\/li>\n<\/ul>\n<ol>\n<li>Basic Regular expressions<\/li>\n<li>Interval Regular expressions (Use option -E for grep and -r for sed)<\/li>\n<li>Extended Regular expressions (Use option -E for grep and -r for sed)<\/li>\n<\/ol>\n<ul>\n<li>What is a Regular expression?<\/li>\n<\/ul>\n<p>A regular expression is a concept of matching a pattern in a given string.<\/p>\n<ul>\n<li>Which commands\/programming languages support regular expressions?<\/li>\n<\/ul>\n<p>vi, tr, rename, grep, sed, awk, perl, python etc.<\/p>\n<h4 id=\"basic-regular-expressions\"><span style=\"color: #993300;\"><b>BASIC REGULAR EXPRESSIONS<\/b><\/span><\/h4>\n<p>Basic regular expressions:<\/p>\n<ul>\n<li>This set includes very basic set of regular expressions which do not require any options to execute.<\/li>\n<li>This set of regular expressions are developed long time back.<\/li>\n<\/ul>\n<p>^ \u2013Caret\/Power symbol to match a starting at the beginning of line.<\/p>\n<p>$ \u2013To match end of the line<\/p>\n<p>* \u20130 or more occurrence of the previous character.<\/p>\n<p>. \u2013To match any character<\/p>\n[] \u2013Range of character<\/p>\n[^char] \u2013negate of occurrence of a character set<\/p>\n<p>&lt;word&gt; \u2013Actual word finding<\/p>\n<p>\u2013Escape character<\/p>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"regular-expression\"><span style=\"color: #99cc00;\"><b>$<\/b><b> REGULAR EXPRESSION<\/b><\/span><\/h4>\n<ul>\n<li>Match all the files which ends with sh<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">  ls -l | grep sh$<\/code><\/pre> <\/div>\n<p>As $ indicates end of the line, the above command will list all the files whose names end with sh.<\/p>\n<p>how about finding lines in a file which ends with dead<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">grep &#039;dead$&#039; filename<\/code><\/pre> <\/div>\n<p>How about finding empty lines in a file?<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">grep &#039;^$&#039; filename<\/code><\/pre> <\/div>\n<h4 id=\"regular-expression-2\"><span style=\"color: #993300;\"><strong>REGULAR EXPRESSION<\/strong><\/span><\/h4>\n<p>Example : Match all files which have a word twt, twet, tweet etc in the file name.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">ls -l | grep &#039;twe*t&#039;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>How about searching for apple word which was spelled wrong in a given file where apple is misspelled as ale, aple, appple, apppple, apppppple etc. To find all patterns<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">grep &#039;ap*le&#039; filename<\/code><\/pre> <\/div>\n<p>Readers should observe that the above pattern will match even ale word as * indicates 0 or more of the previous character occurrence.<\/p>\n<h4 id=\"char-regular-expression\"><span style=\"color: #99cc00;\"><b>[^CHAR] REGULAR EXPRESSION<\/b><\/span><\/h4>\n<p>Example: Match all the file names except a or b or c in it\u2019s filenames<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\"> ls | grep  &#039;[^abc]&#039;<\/code><\/pre> <\/div>\n<p>This will give output all the file names except files which contain a or b or c.<\/p>\n<h4 id=\"word-regular-expression\"><span style=\"color: #ff6600;\"><strong>&lt;WORD&gt; REGULAR EXPRESSION<\/strong><\/span><\/h4>\n<p>Example: Search for a word abc, for example I should not get abcxyz or readabc in my output<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">   grep &#039;&lt;abc&gt;&#039; filename<\/code><\/pre> <\/div>\n<h4 id=\"escape-regular-expression\"><span style=\"color: #99cc00;\"><b>ESCAPE REGULAR EXPRESSION <\/b><\/span><\/h4>\n<p>Example : Find files which contain [ in it\u2019s name, as [ is a special charter we have to escape it<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\"><br\/>grep &quot;[&quot; filename<br\/><br\/>or<br\/><br\/>grep &#039;[[]&#039; filename<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"square-braces-brackets-regular-expression\"><span style=\"color: #993300;\"><b>[]<\/b> <b>SQUARE BRACES\/BRACKETS REGULAR EXPRESSION<\/b><\/span><\/h4>\n<p>Example : Find all the files which contains a number in the file name between a and x<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">ls -l | grep &#039;a[0-9]x&#039;<br\/><br\/>This will find all the files which is<br\/>a0xsdf<br\/>asda1xsdfas<br\/>..<br\/>..<br\/>asdfdsara9xsdf<br\/>etc.<\/code><\/pre> <\/div>\n<ul>\n<li>So where ever it finds a number it will try to match that number.<\/li>\n<li>Some of the range operator examples for\u00a0 you.<\/li>\n<li>[a-z] \u2013Match&#8217;s any single char between a to z.<\/li>\n<li>[A-Z] \u2013Match&#8217;s any single char between A to Z.<\/li>\n<li>[0-9] \u2013Match&#8217;s any single char between 0 to 9.<\/li>\n<li>[a-zA-Z0-9] \u2013 Match&#8217;s any single character either a to z or A to Z or 0 to 9<\/li>\n<li>[!@#$%^] \u2014 Match&#8217;s any ! or @ or # or $ or % or ^ character.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regular expressions (Regexp) is one of the advanced concept we require to write efficient shell scripts and for effective system administration. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[6914,6897,6912,6913,6915,6920,6924,6896,6901,6895,6899,6898,6923,6900,6919,6927,6909,6894,6926,6911,6904,6903,6907,6908,6905,6906,6921,6925,6910,6902,6918,6916,6922,6917],"class_list":["post-3649","post","type-post","status-publish","format-standard","hentry","category-linux","tag-create-regular-expression-online","tag-egrep-regex","tag-grep-command-linux","tag-grep-command-options","tag-grep-in-linux","tag-grep-linux","tag-grep-options","tag-grep-recursive-example","tag-grep-regex-digit","tag-grep-regex-group","tag-grep-regex-tester","tag-grep-regex-whitespace","tag-grep-regular-expression","tag-grep-return-only-match","tag-linux-grep","tag-linux-grep-command","tag-linux-regex-examples","tag-linux-regular-expression-cheat-sheet","tag-man-grep","tag-online-regular-expression","tag-regex-cheat-sheet-c","tag-regex-cheat-sheet-java","tag-regex-cheat-sheet-javascript","tag-regex-cheat-sheet-php","tag-regex-cheat-sheet-python","tag-regex-examples","tag-regex-match","tag-regex-pattern","tag-regular-expression","tag-regular-expression-cheat-sheet-pdf","tag-regular-expression-in-linux","tag-regular-expression-javascript","tag-regular-expression-online","tag-what-is-regular-expression"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3649"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3649\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}