{"id":79073,"date":"2026-11-17T01:52:46","date_gmt":"2026-11-17T01:52:46","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=79073"},"modified":"2026-11-17T01:52:46","modified_gmt":"2026-11-17T01:52:46","slug":"linux-basics-05-text","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=79073","title":{"rendered":"Linux Basics 05 \u2014 Finding Files and Filtering Text"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1990\">Linux Basics category<\/a> \u00b7 Lesson 5 of 8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>AI disclosure<\/strong> \u2014 AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learning goal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choose find for names, grep for lines, cut for fields, and sed for transforming text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concepts and setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The site filters signed integers, tests Canadian postal-code shape, and selects account fields with cut. In the lab we use synthetic data. A postal-code shape check does not prove that a location or postal code exists. LC_ALL=C makes the ASCII-oriented regex and sort behave consistently. Quote patterns so the shell does not expand them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Filter lines and fields without touching system files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>lab=$(mktemp -d)\ncd &quot;$lab&quot; || exit\nprintf '%s\\n' '-7' '+12' '3.5' 'abc' &gt; data.txt\nprintf '%s\\n' 'M4C 5L7' 'm4c 5l7' 'wrong' &gt; postal.txt\nprintf '%s\\n' 'learner:x:1000' 'helper:x:1001' &gt; accounts.txt\nLC_ALL=C grep -E '^[+-]?[0-9]+$' data.txt\nLC_ALL=C grep -E '^[A-Z][0-9][A-Z] ?[0-9][A-Z][0-9]$' postal.txt\ncut -d ':' -f 1,3 accounts.txt | sed 's\/^helper:\/trainee:\/'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-7\n+12\nM4C 5L7\nlearner:1000\ntrainee:1001<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The source uses egrep. grep -E is the straightforward modern spelling. sed writes the replacement to stdout and leaves accounts.txt unchanged.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Filter by file name<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir reports\ncp data.txt reports\/raw.txt\nfind reports -type f -name '*.txt' -print\ngrep -n '3[.]5' reports\/raw.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>reports\/raw.txt\n3:3.5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Quoted *.txt reaches find as a pattern. In contrast, grep -n displays the line number of a content match.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Questions only<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>What do ^ and $ mean in this regex?<\/li>\n\n\n<li>Why did the plain sed command leave the file unchanged?<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>Change the signed-integer pattern to accept only unsigned integers.<\/li>\n\n\n<li>Use cut to print the username and ID from colon-delimited accounts.txt.<\/li>\n\n\n<li>Distinguish the three exit statuses of GNU grep in ordinary use.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Questions and answers<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check answers<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>What do ^ and $ mean in this regex?<\/strong><br><strong>Answer<\/strong> \u2014 They anchor the start and end of a line.<\/li>\n\n\n<li><strong>Why did the plain sed command leave the file unchanged?<\/strong><br><strong>Answer<\/strong> \u2014 It prints transformed text to standard output without in-place editing.<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment answer key<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>Change the signed-integer pattern to accept only unsigned integers.<\/strong><br><strong>Answer<\/strong> \u2014 Use LC_ALL=C grep -E &#8216;^[0-9]+$&#8217; data.txt. None of the four starter lines match.<\/li>\n\n\n<li><strong>Use cut to print the username and ID from colon-delimited accounts.txt.<\/strong><br><strong>Answer<\/strong> \u2014 cut -d &#8216;:&#8217; -f 1,3 accounts.txt.<\/li>\n\n\n<li><strong>Distinguish the three exit statuses of GNU grep in ordinary use.<\/strong><br><strong>Answer<\/strong> \u2014 0 means match, 1 means no match, and 2 indicates an error in ordinary use.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Source articles on this site<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=76448\">Linux: Regular Expression: grep<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=76440\">Linux sed, cat, cut examples<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=76438\">Linux: sed: search, replace, delete, print<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=9594\">\u0987\u0989\u09a8\u09bf\u0995\u09cd\u09b8\u0983 \u09aa\u09be\u0987\u09aa \u098f\u09ac\u0982 \u09ab\u09bf\u09b2\u09cd\u099f\u09be\u09b0 (Unix \u2013 Pipes and Filters)<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Continue the course<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Previous lesson 04 \u2014 Pipes, Redirection, and Exit Status. Next lesson 06 \u2014 File Permissions and umask.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1990\">Linux Basics category<\/a> \u00b7 Lesson 5 of 8<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Choose find for names, grep for lines, cut for fields, and sed for transforming text.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1990],"tags":[],"class_list":["post-79073","post","type-post","status-publish","format-standard","hentry","category-linux-basics","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":79069,"url":"http:\/\/bangla.sitestree.com\/?p=79069","url_meta":{"origin":79073,"position":0},"title":"Linux Basics 01 \u2014 Linux, the Shell, and Your First Lab","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Tell the kernel, distribution, terminal, and shell apart, then identify the system you are actually using.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79071,"url":"http:\/\/bangla.sitestree.com\/?p=79071","url_meta":{"origin":79073,"position":1},"title":"Linux Basics 03 \u2014 Editing Text and Reading Files","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Edit a text file in a terminal and verify the exact lines you saved.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79072,"url":"http:\/\/bangla.sitestree.com\/?p=79072","url_meta":{"origin":79073,"position":2},"title":"Linux Basics 04 \u2014 Pipes, Redirection, and Exit Status","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Route input, normal output, and errors, then detect a command failure inside a pipeline.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79102,"url":"http:\/\/bangla.sitestree.com\/?p=79102","url_meta":{"origin":79073,"position":3},"title":"Linux Administration Course \u2014 Start Here and Lesson Index","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"A 33-lesson Linux course from shell fundamentals to server administration, performance, and a recovery capstone.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79074,"url":"http:\/\/bangla.sitestree.com\/?p=79074","url_meta":{"origin":79073,"position":4},"title":"Linux Basics 06 \u2014 File Permissions and umask","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Read a permission string, predict umask results, and choose a minimal permission change.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79070,"url":"http:\/\/bangla.sitestree.com\/?p=79070","url_meta":{"origin":79073,"position":5},"title":"Linux Basics 02 \u2014 Paths, Files, and Links","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Navigate confidently, copy and rename a file, and explain the difference between hard links and symbolic links.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79073"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79073\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79073"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}