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

Time:09-09

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.

Does anybody have a Powershell solution for this?

CodePudding user response:

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!

  • Related