{"id":170480,"date":"2021-02-03T11:00:43","date_gmt":"2021-02-03T08:00:43","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/"},"modified":"2021-02-03T11:00:43","modified_gmt":"2021-02-03T08:00:43","slug":"how-to-process-a-file-line-by-line-in-a-linux-bash-script","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/","title":{"rendered":"#How to Process a File Line by Line in a Linux Bash Script"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a419a1662986\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a419a1662986\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/#Files_Text_and_Idioms\" >Files, Text, and Idioms<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/#Reading_Lines_From_a_File_The_One-Liner\" >Reading Lines From a File: The One-Liner<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/#Reading_Lines_From_a_File_With_a_Script\" >Reading Lines From a File With a Script<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/#Passing_the_Line_to_a_Function\" >Passing the Line to a Function<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/#Building_Blocks_Are_Useful\" >Building Blocks Are Useful<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Process a File Line by Line in a Linux Bash <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>&#8221;<\/strong><\/p>\n<div>\n<figure style=\"width: 650px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-442612 size-full\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/09\/stock-lede-linux-see-attribution.png\" alt=\"A terminal window on a Linux computer system.\" width=\"650\" height=\"300\" data-credittext=\"Fatmawati Achmad Zaenuri\/Shutterstock\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\">Fatmawati Achmad Zaenuri\/Shutterstock<\/a><\/span><\/figcaption><\/figure>\n<p>It\u2019s pretty easy to read the contents of a Linux text file line by line in a shell script\u2014as long as you deal with some subtle gotchas. Here\u2019s how to do it the safe way.<\/p>\n<h3 id=\"files-text-and-idioms\"><span class=\"ez-toc-section\" id=\"Files_Text_and_Idioms\"><\/span>Files, Text, and Idioms<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Each programming language has a set of idioms. These are the standard, no-frills ways to accomplish a set of common tasks. They\u2019re the elementary or default way to use one of the features of the language the programmer is working with. They become part of a programmer\u2019s toolkit of mental blueprints.<\/p>\n<p>Actions like reading data from files, working with loops, and swapping the values of two variables are good examples. The programmer will know at least one way to achieve their ends in a generic or vanilla fashion. Perhaps that will suffice for the requirement at hand. Or maybe they\u2019ll embellish the code to make it more efficient or applicable to the specific solution they are developing. But having the building-block idiom at their fingertips is a great starting point.<\/p>\n<p>Knowing and understanding idioms in one language makes it easier to pick up a new programming language, too. Knowing how things are constructed in one language and looking for the equivalent\u2014or the closest thing\u2014in another language is a good way to appreciate the similarities and differences between programming languages you already know and the one you\u2019re learning.<\/p>\n<h3 id=\"the-simplest-way-to-read-lines-from-a-file\"><span class=\"ez-toc-section\" id=\"Reading_Lines_From_a_File_The_One-Liner\"><\/span>Reading Lines From a File: The One-Liner<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In Bash, you can use a <code>while<\/code> loop on the command line to read each line of text from a file and do something with it. Our text file is called \u201cdata.txt.\u201d It holds a list of the months of the year.<\/p>\n<pre>January&#13;\nFebruary&#13;\nMarch&#13;\n.&#13;\n.&#13;\nOctober&#13;\nNovember&#13;\nDecember<\/pre>\n<p>Our simple one-liner is:<\/p>\n<pre>while read line; do echo $line; done &lt; data.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-710544\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/01\/1-2.png\" alt=\"\" width=\"642\" height=\"307\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The <code>while<\/code> loop reads a line from the file, and the execution flow of the little program passes to the body of the loop. The <code>echo<\/code> command writes the line of text in the terminal window. The read attempt fails when there are no more lines to be read, and the loop is done.<\/p>\n<p>One neat trick is the ability\u00a0to redirect a file into a loop. In other programming languages, you\u2019d need to open the file, read from it, and close it again when you\u2019d finished. With Bash, you can simply use file redirection and let the shell handle all of that low-level stuff for you.<\/p>\n<p>Of course, this one-liner isn\u2019t terribly useful. Linux already provides the <code>cat<\/code> command, which does exactly that for us. We\u2019ve created a long-winded way to replace a three-letter command. But it does visibly demonstrate the principles of reading from a file.<\/p>\n<p>That works well enough, up to a point. Suppose we have another text file that contains the names of the months. In this file, the escape sequence for a newline character has been appended to each line. We\u2019ll call it \u201cdata2.txt.\u201d<\/p>\n<pre>Januaryn&#13;\nFebruaryn&#13;\nMarchn&#13;\n.&#13;\n.&#13;\nOctobern&#13;\nNovembern&#13;\nDecembern<\/pre>\n<p>Let\u2019s use our one-liner on our new file.<\/p>\n<pre>while read line; do echo $line; done &lt; data2.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-710545\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/01\/2-2.png\" alt=\"\" width=\"646\" height=\"307\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The backslash escape character \u201d <code><\/code> \u201d has been discarded. The result is that an \u201cn\u201d has been appended to each line. Bash is interpreting the backslash as the start of an <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Escape_sequence\">escape sequence<\/a>. Often, we don\u2019t want Bash to interpret what it is reading. It can be more convenient to read a line in its entirety\u2014backslash escape sequences and all\u2014and choose what to parse out or replace yourself, within your own code.<\/p>\n<p>If we want to do any meaningful processing or parsing on the lines of text, we\u2019ll need to use a script.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Reading_Lines_From_a_File_With_a_Script\"><\/span>Reading Lines From a File With a Script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s our script. It\u2019s called \u201cscript1.sh.\u201d<\/p>\n<pre id=\"cb1\" class=\"sourceCode\"><code class=\"sourceCode bash\"><span id=\"cb1-1\"><span class=\"co\">#!\/bin\/bash<\/span><\/span>&#13;\n<span id=\"cb1-2\"\/>&#13;\n<span id=\"cb1-3\"><span class=\"va\">Counter=<\/span>0<\/span>&#13;\n<span id=\"cb1-4\"\/>&#13;\n<span id=\"cb1-5\"><span class=\"cf\">while<\/span> <span class=\"va\">IFS=<\/span><span class=\"st\">''<\/span> <span class=\"bu\">read<\/span> <span class=\"at\">-r<\/span> <span class=\"va\">LinefromFile<\/span> <span class=\"kw\">||<\/span> <span class=\"kw\">[[<\/span> <span class=\"ot\">-n<\/span> <span class=\"st\">\"<\/span><span class=\"va\">${LinefromFile}<\/span><span class=\"st\">\"<\/span> <span class=\"kw\">]];<\/span> <span class=\"cf\">do<\/span><\/span>&#13;\n<span id=\"cb1-6\"\/>&#13;\n<span id=\"cb1-7\">    <span class=\"kw\">((<\/span><span class=\"va\">Counter<\/span><span class=\"op\">++<\/span><span class=\"kw\">))<\/span><\/span>&#13;\n<span id=\"cb1-8\">    <span class=\"bu\">echo<\/span> <span class=\"st\">\"Accessing line <\/span><span class=\"va\">$Counter<\/span><span class=\"st\">: <\/span><span class=\"va\">${LinefromFile}<\/span><span class=\"st\">\"<\/span><\/span>&#13;\n<span id=\"cb1-9\"\/>&#13;\n<span id=\"cb1-10\"><span class=\"cf\">done<\/span> <span class=\"op\">&lt;<\/span> <span class=\"st\">\"<\/span><span class=\"va\">$1<\/span><span class=\"st\">\"<\/span><\/span><\/code><\/pre>\n<p>We set a variable called <code>Counter<\/code> to zero, then we define our <code>while<\/code> loop.<\/p>\n<p>The first statement on the while line is <code>IFS=''<\/code> . <code>IFS<\/code> stands for internal field separator. It holds values that Bash uses to identify word boundaries. By default, the read command s<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a>s off leading and trailing whitespace. If we want to read the lines from the file exactly as they are, we need to set <code>IFS<\/code> to be an empty string.<\/p>\n<p>We could set this once outside of the loop, just like we\u2019re setting the value of <code>Counter<\/code> . But with more complex scripts\u2014especially those with many user-defined functions in them\u2014it is possible that <code>IFS<\/code> could be set to different values elsewhere in the script. Ensuring that <code>IFS<\/code> is set to an empty string each time the <code>while<\/code> loop iterates guarantees that we know what its behavior will be.<\/p>\n<p>We\u2019re going to read a line of text into a variable called <code>LinefromFile<\/code> . We\u2019re using the <code>-r<\/code> (read backslash as a normal character) option to ignore backslashes. They\u2019ll be treated just like any other character and won\u2019t receive any special treatment.<\/p>\n<p>There are two conditions that will satisfy the <code>while<\/code> loop and allow the text to be processed by the body of the loop:<\/p>\n<ul>\n<li><code>read -r LinefromFile<\/code> : When a line of text is successfully read from the file, the <code>read<\/code> command sends a success signal to the <code>while<\/code>\u00a0, and the <code>while<\/code> loop passes the execution flow to the body of the loop. Note that the <code>read<\/code> command needs to see a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Newline\">newline character<\/a> at the end of the line of text in order to consider it a successful read.\u00a0If the file is not a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/POSIX\">POSIX<\/a> compliant text file, the\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/pubs.opengroup.org\/onlinepubs\/9699919799\/basedefs\/V1_chap03.html#tag_03_206\">last line may not include a newline character<\/a>.\u00a0If the <code>read<\/code> command sees the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/End-of-file\">end of file marker<\/a> (EOF) before the line is terminated by a newline, it will <em>not<\/em> treat it as a successful read. If that happens, the last line of text will not be passed to the body of the loop and will not be processed.<\/li>\n<li><code>[ -n \"${LinefromFile}\" ]<\/code> : We need to do some extra work to handle non-POSIX compatible files. This comparison checks the text that is read from the file. If it isn\u2019t terminated with a newline character, this comparison will still return success to the <code>while<\/code> loop. This ensures that any trailing line fragments are processed by the body of the loop.<\/li>\n<\/ul>\n<p>These two clauses are separated by the OR logical operator \u201d <code>||<\/code> \u201d so that if\u00a0<em>either<\/em>\u00a0clause returns success, the retrieved text is processed by the body of the loop, whether there is a newline character or not.<\/p>\n<p>In the body of our loop, we\u2019re incrementing the <code>Counter<\/code> variable by one and using <code>echo<\/code> to send some output to the terminal window. The line number and the text of each line are displayed.<\/p>\n<p>We can still use our redirection trick to redirect a file into a loop. In this case, we\u2019re redirecting $1, a variable that holds the name of the first command line parameter that passed to the script. Using this trick, we can easily pass in the name of the data file that we want the script to work on.<\/p>\n<p>Copy and paste the script into an editor and save it with the filename \u201cscript1.sh.\u201d Use the <code>chmod<\/code> command <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/chmod.1.html\">to make it executable<\/a>.<\/p>\n<pre>chmod +x script1.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-710546\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/01\/3-2.png\" alt=\"\" width=\"646\" height=\"57\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Let\u2019s see what our script makes of the data2.txt text file and the backslashes contained within it.<\/p>\n<pre>.\/script1.sh data2.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-710547\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/01\/4-2.png\" alt=\"\" width=\"646\" height=\"307\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Every character in the line is displayed verbatim. The backslashes are not interpreted as escape characters. They\u2019re printed as regular characters.<\/p>\n<h3 id=\"passing-the-line-to-a-function\"><span class=\"ez-toc-section\" id=\"Passing_the_Line_to_a_Function\"><\/span>Passing the Line to a Function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We\u2019re still just echoing the text to the screen. In a real-world programming scenario, we\u2019d likely be about to do something more interesting with the line of text. In most cases, it is a good programming practice to handle the further processing of the line in another function.<\/p>\n<p>Here\u2019s how we could do it. This is \u201cscript2.sh.\u201d<\/p>\n<div id=\"cb2\" class=\"sourceCode\">\n<pre class=\"sourceCode bash\"><code class=\"sourceCode bash\"><span id=\"cb2-1\"><span class=\"co\">#!\/bin\/bash<\/span><\/span>&#13;\n<span id=\"cb2-2\"\/>&#13;\n<span id=\"cb2-3\"><span class=\"va\">Counter=<\/span>0<\/span>&#13;\n<span id=\"cb2-4\"\/>&#13;\n<span id=\"cb2-5\"><span class=\"kw\">function<\/span><span class=\"fu\"> process_line()<\/span> <span class=\"kw\">{<\/span><\/span>&#13;\n<span id=\"cb2-6\"\/>&#13;\n<span id=\"cb2-7\">    <span class=\"bu\">echo<\/span> <span class=\"st\">\"Processing line <\/span><span class=\"va\">$Counter<\/span><span class=\"st\">: <\/span><span class=\"va\">$1<\/span><span class=\"st\">\"<\/span><\/span>&#13;\n<span id=\"cb2-8\"\/>&#13;\n<span id=\"cb2-9\"><span class=\"kw\">}<\/span><\/span>&#13;\n<span id=\"cb2-10\"\/>&#13;\n<span id=\"cb2-11\"><span class=\"cf\">while<\/span> <span class=\"va\">IFS=<\/span><span class=\"st\">''<\/span> <span class=\"bu\">read<\/span> <span class=\"at\">-r<\/span> <span class=\"va\">LinefromFile<\/span> <span class=\"kw\">||<\/span> <span class=\"kw\">[[<\/span> <span class=\"ot\">-n<\/span> <span class=\"st\">\"<\/span><span class=\"va\">${LinefromFile}<\/span><span class=\"st\">\"<\/span> <span class=\"kw\">]];<\/span> <span class=\"cf\">do<\/span><\/span>&#13;\n<span id=\"cb2-12\"\/>&#13;\n<span id=\"cb2-13\">    <span class=\"kw\">((<\/span><span class=\"va\">Counter<\/span><span class=\"op\">++<\/span><span class=\"kw\">))<\/span><\/span>&#13;\n<span id=\"cb2-14\">    <span class=\"ex\">process_line<\/span> <span class=\"st\">\"<\/span><span class=\"va\">$LinefromFile<\/span><span class=\"st\">\"<\/span><\/span>&#13;\n<span id=\"cb2-15\"\/>&#13;\n<span id=\"cb2-16\"><span class=\"cf\">done<\/span> <span class=\"op\">&lt;<\/span> <span class=\"st\">\"<\/span><span class=\"va\">$1<\/span><span class=\"st\">\"<\/span><\/span><\/code><\/pre>\n<\/div>\n<p>We define our <code>Counter<\/code> variable as before, and then we define a function called <code>process_line()<\/code> . The definition of a function must appear <em>before<\/em> the function is first called in the script.<\/p>\n<p>Our function is going to be passed the newly read line of text in each iteration of the <code>while<\/code> loop. We can access that value within the function by using the <code>$1<\/code> variable. If there were two variables passed to the function, we could access those values using <code>$1<\/code> and <code>$2<\/code> , and so on for more variables.<\/p>\n<p>The w<code>hile<\/code>\u00a0loop is mainly the same. There is only one change inside the body of the loop. The <code>echo<\/code> line has been replaced by a call to the <code>process_line()<\/code> function. Note that you don\u2019t need to use the \u201c()\u201d brackets in the name of the function when you are calling it.<\/p>\n<p>The name of the variable holding the line of text, <code>LinefromFile<\/code> , is wrapped in quotation marks when it is passed to the function. This caters for lines that have spaces in them. Without the quotation marks, the first word is treated as <code>$1<\/code> by the function, the second word is considered to be <code>$2<\/code> , and so on. Using quotation marks ensures that the entire line of text is handled, altogether, as <code>$1<\/code>. Note that this is <em>not<\/em> the same <code>$1<\/code> that holds the same data file passed to the script.<\/p>\n<p>Because <code>Counter<\/code> has been declared in the main body of the script and not inside a function, it can be referenced inside the <code>process_line()<\/code> function.<\/p>\n<p>Copy or type the script above into an editor and save it with the filename \u201cscript2.sh.\u201d Make it executable with <code>chmod<\/code> :<\/p>\n<pre>chmod +x script2.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-710548\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/01\/5-2.png\" alt=\"\" width=\"646\" height=\"57\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Now we can run it and pass in a new data file, \u201cdata3.txt.\u201d This has a list of the months in it, and one line with many words on it.<\/p>\n<pre>January&#13;\nFebruary&#13;\nMarch&#13;\n.&#13;\n.&#13;\nOctober&#13;\nNovember nMore text \"at the end of the line\"&#13;\nDecember<\/pre>\n<p>Our command is:<\/p>\n<pre>.\/script2.sh data3.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-710552\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/01\/6-2.png\" alt=\"\" width=\"646\" height=\"307\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The lines are read from the file and passed one by one to the <code>process_line()<\/code> function. All the lines are displayed correctly, including the odd one with the backspace, quotation marks, and multiple words in it.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Building_Blocks_Are_Useful\"><\/span>Building Blocks Are Useful<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There\u2019s a train of thought that says that an idiom must contain something unique to that language. That\u2019s not a belief that I subscribe to. What\u2019s important is that it makes good use of the language, is easy to remember, and provides a reliable and robust way to implement some functionality in your code.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/709838\/how-to-process-a-file-line-by-line-in-a-linux-bash-script\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Process a File Line by Line in a Linux Bash Script&#8221; Fatmawati Achmad Zaenuri\/Shutterstock It\u2019s pretty easy to read the contents of a Linux text file line by line in a shell script\u2014as long as you deal with some subtle gotchas. Here\u2019s how to do it the safe way. Files, Text, and Idioms&#8230;<\/p>\n","protected":false},"author":1,"featured_media":170481,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2019\/09\/stock-lede-linux-see-attribution.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-170480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/170480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=170480"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/170480\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/170481"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=170480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=170480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=170480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}