{"id":3507,"date":"2014-08-27T18:57:23","date_gmt":"2014-08-27T10:57:23","guid":{"rendered":"http:\/\/rmohan.com\/?p=3507"},"modified":"2014-08-27T18:57:23","modified_gmt":"2014-08-27T10:57:23","slug":"ifs-internal-field-separator","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3507","title":{"rendered":"IFS \u2013 Internal Field Separator"},"content":{"rendered":"<p>It seems like an esoteric concept, but it\u2019s actually very useful.<\/p>\n<p>If your input file is \u201c1 apple steve@example.com\u201d, then your script could say:<\/p>\n<p>while read qty product customer<br \/>\ndo<br \/>\n  echo &#8220;${customer} wants ${qty} ${product}(s)&#8221;<br \/>\ndone<br \/>\nThe read command will read in the three variables, because they\u2019re spaced out from each other.<\/p>\n<p>However, critical data is often presented in spreadsheet format. If you save these as CSV files, it will come out like this:<\/p>\n<p>1,apple,steve@example.com<br \/>\nThis contains no spaces, and the above code will not be able to understand it. It will take the whole thing as one item \u2013 the first thing, quanity, $qty, and set the other two fields as blank.<\/p>\n<p>The way around this, is to tell the entire shell, that \u201c,\u201d (the comma itself) separates fields; it\u2019s the \u201cinternal field separator\u201d, or IFS.<\/p>\n<p>The IFS variable is set to space\/tab\/newline, which isn\u2019t easy to set in the shell, so it\u2019s best to save the original IFS to another variable, so you can put it back again after you\u2019ve messed around with it. I tend to use \u201coIFS=$IFS\u201d to save the current value into \u201coIFS\u201d.<\/p>\n<p>Also, when the IFS variable is set to something other than the default, it can really mess with other code.<\/p>\n<p>Here\u2019s a script I wrote today to parse a CSV file:<\/p>\n<p>#!\/bin\/sh<br \/>\noIFS=$IFS     # Always keep the original IFS!<br \/>\nIFS=&#8221;,&#8221;          # Now set it to what we want the &#8220;read&#8221; loop to use<br \/>\nwhile read qty product customer<br \/>\ndo<br \/>\n  IFS=$oIFS<br \/>\n  # process the information<br \/>\n  IFS=&#8221;,&#8221;       # Put it back to the comma, for the loop to go around again<br \/>\ndone < myfile.txt\nIt really is that easy, and it\u2019s very versatile. You do have to be careful to keep a copy of the original (I always use the name oIFS, but whatever suits you), and to put it back as soon as possible, because so many things invisibly use the IFS \u2013 grep, cut, you name it. It\u2019s surprising how many things within the \u201cwhile read\u201d loop actually did depend on the IFS being the default value.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It seems like an esoteric concept, but it\u2019s actually very useful.<\/p>\n<p>If your input file is \u201c1 apple steve@example.com\u201d, then your script could say:<\/p>\n<p>while read qty product customer do echo &#8220;${customer} wants ${qty} ${product}(s)&#8221; done The read command will read in the three variables, because they\u2019re spaced out from each other.<\/p>\n<p>However, critical data [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3507"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3507"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3507\/revisions"}],"predecessor-version":[{"id":3508,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3507\/revisions\/3508"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}