Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a file of numbers based on a file containing count of numbers (tally)

I have a requirement to convert a .csv file containing data like this:

100,3
101,2
102,4

to a csv. file containing this:

100
100
100
101
101
102
102
102
102

I’ve written a macro in Excel that does this but the requirement is to carry this out against ~1 million records which crashes Excel.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does anybody have a Powershell solution for this?

>Solution :

Assuming the CSV does not contain headers, I’d do the following:

Import-Csv tally.txt -Header Number,Tally |ForEach-Object {
  ,$_.Number * $_.Tally
} |Set-Content output.txt

The expression ,"100" * "2" will cause PowerShell to produce an array consisting of 2 copies of the string "100" – exactly the kind of expansion we want!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading