{"id":816,"date":"2020-08-24T22:34:50","date_gmt":"2020-08-24T17:04:50","guid":{"rendered":"https:\/\/techieshouts.com\/?p=816"},"modified":"2022-08-09T19:05:02","modified_gmt":"2022-08-09T13:35:02","slug":"reading-a-delimited-file-in-shell-script","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/reading-a-delimited-file-in-shell-script\/","title":{"rendered":"Reading a delimited file in Shell script"},"content":{"rendered":"\n<p>In the ETL world, it is always a need for processing the delimited files. While reading a delimited file can be done through all the scripts and programming languages, we will see how this can be done in a shell script using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cut_(Unix)\" target=\"_blank\" rel=\"noopener\">cut command<\/a><\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$(cut -d \"|\" -f1 &lt;Delimited file> | tr -d '[:space:]')<\/pre>\n\n\n\n<p><strong>cut -d &#8220;|&#8221;<\/strong>  =&gt; This is to tell the command that the delimiter is &#8220;|&#8221; for this operation<\/p>\n\n\n\n<p><strong>-f1 &lt;Delimited file&gt;<\/strong> =&gt; This will read the first column from the delimited file<\/p>\n\n\n\n<p><strong>tr -d &#8216;[:space:]&#8217;<\/strong> =&gt; This will delete the extra spaces at the end of the values<\/p>\n\n\n\n<p>Let us see it with a real-time example. In the following example, we will see how to read the delimited content and assign it to variables. The sample file is &#8220;delimitedfile.txt&#8221;<\/p>\n\n\n\n<h4>delimitedfile.txt<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ cat delimitedfile.txt\n1|Sankar|Sales|2020=08-01|Chennai<\/pre>\n\n\n\n<p>Let us see a shell script that reads and displays the above-delimited file.<\/p>\n\n\n\n<h4>filereader.sh<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export sourcefile=\"delimitedfile.txt\"\necho \"Source file to read = $sourcefile\"\n\necho \"Program to read delimited file starts...\"\nexport id=$(cut -d \"|\" -f1 ${sourcefile} | tr -d '[:space:]')\nexport name=$(cut -d \"|\" -f2 ${sourcefile} | tr -d '[:space:]')\nexport department=$(cut -d \"|\" -f3 ${sourcefile} | tr -d '[:space:]')\nexport joineddate=$(cut -d \"|\" -f4 ${sourcefile} | tr -d '[:space:]')\nexport city=$(cut -d \"|\" -f5 ${sourcefile} | tr -d '[:space:]')\n\necho \"\"\necho \"ID                :       $id\"\necho \"NAME              :       $name\"\necho \"DEPT              :       $department\"\necho \"JOINED ON         :       $joineddate\"\necho \"CITY              :       $city\"\necho \"\"<\/pre>\n\n\n\n<p>When you run the above script you will see the result as follows.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sh filreader.sh\nSource file to read = delimitedfile.txt\nProgram to read delimited file starts...\n\nID\t\t    :\t1\nNAME\t\t:\tSankar\nDEPT\t\t:\tSales\nJOINED ON\t:\t2020=08-01\nCITY\t\t:\tChennai\n<\/pre>\n\n\n\n<p>Also check &#8220;<a href=\"https:\/\/techieshouts.com\/for-loop-in-shell-script-over-string-list\/\">How to iterate string list using for loop in shell<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the ETL world, it is always a need for processing the delimited files. While reading a delimited file can be done through all the scripts and programming languages, we will see how this can be done in a shell script using the cut command Syntax cut -d &#8220;|&#8221; =&gt; This is to tell the\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/reading-a-delimited-file-in-shell-script\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,8],"tags":[100,101],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/816"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=816"}],"version-history":[{"count":8,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/816\/revisions"}],"predecessor-version":[{"id":834,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/816\/revisions\/834"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}