Convert CSV to this new format -
i have csv file sorted country, state, , city. looks this:
usa,california,arcata usa,california,bakersfield usa,california,barstow usa,california,berkeley usa,california,chico usa,california,delano usa,california,el centro usa,california,eureka usa,california,fresno usa,california,irvine usa,colorado,colorado springs usa,colorado,denver usa,colorado,durango usa,colorado,fort collins
i want create output file looks this:
usa -california --bakersfield --barstow --berkeley --chico --delano --el centro --eureka --fresno --irvine -colorado --colorado springs --denver --durango --fort collins
states prepended single dash, , cities prepended 2 dashes within each state.
how create output? thanks!
please check below script in powershell. have made assumption there 1 country. entries can jumbled in way.
also, assume there below row first row in csv headers:
country,state,city
you can use similar logic in other programming language well.
$csvinput = import-csv -path "c:\data\test\abc.csv" | sort-object state $outputfilecontent = @() $outputfilecontent += ($csvinput[0].country) $oldstate = "" $newstate = "" foreach($csvrow in $csvinput) { $newstate = $csvrow.state if($newstate -ne $oldstate) { $outputfilecontent+= ( "-" + $csvrow.state) } $oldstate = $newstate $outputfilecontent += ("--" + $csvrow.city) } $outputfilecontent | out-file -filepath "c:\data\test\testoutput.txt"
replace "c:\data\test\abc.csv" input csv file , "c:\data\test\testoutput.txt" path output file.
Comments
Post a Comment