{"id":91,"date":"2009-09-02T06:31:05","date_gmt":"2009-09-02T06:31:05","guid":{"rendered":"http:\/\/www.adriangrigoras.com\/blog\/?p=91"},"modified":"2009-09-02T06:31:05","modified_gmt":"2009-09-02T06:31:05","slug":"change-a-csv-file","status":"publish","type":"post","link":"https:\/\/adriangrigoras.com\/blog\/change-a-csv-file\/","title":{"rendered":"Change a csv file"},"content":{"rendered":"<p>If you want to use a huge csv file, an easy way to do this is to parse it and write another file. Bellow is an example, which takes the values from old_file.csv, do some changes and write another file new_file.csv<\/p>\n<blockquote>\n<address>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">#! \/usr\/bin\/perl<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$year=1900+$yearOffset;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$time=&#8221;$year-$month-$dayOfMonth-$hour$minute$second&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">print &#8220;Started at $timenn&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">open (MYFILE, &#8220;&gt;&gt;&#8221;, &#8216;parse_logs.txt&#8217;);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">print MYFILE &#8220;Started at $timen&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">close (MYFILE);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$i=0;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">open (FINAL, &#8220;&gt;&#8221;, &#8216;new_file.csv&#8217;);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">open (FILE, &#8220;&lt;&#8220;, &#8216;old_file.csv&#8217;);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">@lines=&lt;FILE&gt;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">foreach $l(@lines){<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$i++;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">print &#8220;$i n&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">@values=split(&#8216;,&#8217;,$l);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$values[1]=$i; #asign a new ID<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$values[2]=~s\/s*\/\/g; # remove all the whitespace<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">#other changes<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$l=join(&#8216;,&#8217;,@values);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">print FINAL &#8220;$l&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">}<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">close (FILE);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">close (FINAL);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$year=1900+$yearOffset;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">$time=&#8221;$year-$month-$dayOfMonth-$hour$minute$second&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">print &#8220;Finished at $timenn&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">open (MYFILE, &#8220;&gt;&gt;&#8221;, &#8216;parse_logs.txt&#8217;);<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">print MYFILE &#8220;Started at $timen&#8221;;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">close (MYFILE);<\/div>\n<address>#! \/usr\/bin\/perl<\/address>\n<address><\/address>\n<address>($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();<\/address>\n<address>$year=1900+$yearOffset;<\/address>\n<address>$time=&#8221;$year-$month-$dayOfMonth-$hour$minute$second&#8221;;<\/address>\n<address><\/address>\n<address>print &#8220;Started at $timenn&#8221;;<\/address>\n<address><\/address>\n<address>open (MYFILE, &#8220;&gt;&gt;&#8221;, &#8216;parse_logs.txt&#8217;);<\/address>\n<address>print MYFILE &#8220;Started at $timen&#8221;;<\/address>\n<address>close (MYFILE);<\/address>\n<address>$i=0;<\/address>\n<address><\/address>\n<address>open (FINAL, &#8220;&gt;&#8221;, &#8216;new_file.csv&#8217;);<\/address>\n<address><\/address>\n<address>open (FILE, &#8220;&lt;&#8220;, &#8216;old_file.csv&#8217;);<\/address>\n<address>@lines=&lt;FILE&gt;;<\/address>\n<address>foreach $l(@lines){<\/address>\n<address> $i++;<\/address>\n<address> print &#8220;$i n&#8221;;<\/address>\n<address> @values=split(&#8216;,&#8217;,$l);<\/address>\n<address> $values[1]=$i; #asign a new ID<\/address>\n<address> $values[2]=~s\/s*\/\/g; # remove all the whitespace<\/address>\n<address> #other changes<\/address>\n<address> $l=join(&#8216;,&#8217;,@values);<\/address>\n<address> print FINAL &#8220;$l&#8221;;<\/address>\n<address>}<\/address>\n<address><\/address>\n<address>close (FILE);<\/address>\n<address>close (FINAL);<\/address>\n<address>($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();<\/address>\n<address>$year=1900+$yearOffset;<\/address>\n<address>$time=&#8221;$year-$month-$dayOfMonth-$hour$minute$second&#8221;;<\/address>\n<address>print &#8220;Finished at $timenn&#8221;;<\/address>\n<address><\/address>\n<address>open (MYFILE, &#8220;&gt;&gt;&#8221;, &#8216;parse_logs.txt&#8217;);<\/address>\n<address>print MYFILE &#8220;Started at $timen&#8221;;<\/address>\n<address>close (MYFILE);<\/address>\n<div><\/div>\n<\/address>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>If you want to use a huge csv file, an easy way to do this is to parse it and write another file. Bellow is an example, which takes the values from old_file.csv, do some changes and write another file new_file.csv #! \/usr\/bin\/perl ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(); $year=1900+$yearOffset;\u2026 <span class=\"read-more\"><a href=\"https:\/\/adriangrigoras.com\/blog\/change-a-csv-file\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":0,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}