Tell me how to cut several columns from one CSV file and create a new CSV file consisting of only these columns?
Requires 4, 5, 7, 11 columns. Separator ','.

  • 2
    What does it mean to write them in another? Generate a new csv, consisting only of these columns? Or add them to an existing one? - Sublihim
  • To form a new csv, consisting only of these columns - Caimans
  • and the columns have headers? - Sublihim

1 answer 1

If your columns are named, for example 'company,' price ', product' and so on, then everything is simple. Here is the script that I use to cut the data that the technician needs:

# Папка, где лежат данные, можно переделать, чтобы вводить в качестве аргумента $inp = 'H:\result\csv_data\current\' # Флэшка лаборанта, всегда L :) тоже можно переделать # и вообще, если вам нужен конкретный файл, сразу используйте его, а не циклите $out = 'L:\' # выборка столбцов $cols = 'date','pacient_name', 'status','level' # циклим по файлам в папке Get-ChildItem $folderPath -Name | ForEach-Object { $fileinp = $inp + $_ $fileout = $out + $_ # выбираем столбцы, записываем их в новый файл на флэшку Import-Csv $fileinp | Select $cols | Export-Csv -Path $fileout –NoTypeInformation } 

If your columns are not dereferenced, then you will have to do this, and the script will have to be redone. It will then run longer:

 # Папка, где лежат данные, можно переделать, чтобы вводить в качестве аргумента $inp = 'H:\result\csv_data\current\' # Флэшка лаборанта, всегда L :) тоже можно переделать # и вообще, если вам нужен конкретный файл, сразу используйте его, а не циклите $out = 'L:\' # выборка столбцов, вам нужны 4,5,7,11 $cols = 'four','five', 'seven','eleven' # циклим по файлам в папке Get-ChildItem $folderPath -Name | ForEach-Object { $fileinp = $inp + $_ $fileout = $out + $_ # выбираем столбцы, записываем их в новый файл на флэшку # поскольку последний из нужных столбцов - одиннадцатый, я беру максимум 11 столбцов Import-Csv $fileinp -header one,two,three,four,five,six,seven,eight,nine,ten,eleven | Select $cols | Export-Csv -Path $fileout –NoTypeInformation } 

In general, if you do not need dancings with folders, but you just need to process one file in this way, save the script below, for example, with the name select.ps1 and then use as select.ps1 input_file_name.csv output_file_name.csv

 $cols = 'four','five', 'seven','eleven' Import-Csv $args[0] -header one,two,three,four,five,six,seven,eight,nine,ten,eleven | Select $cols | Export-Csv -Path $args[1] –NoTypeInformation 

PS Let me remind you that in Windows 10 / Server 16 (and, possibly, server 12) powershell will send you away without permission to execute scripts. To cope with this, resolve the scripts for the duration of the session via Set-ExecutionPolicy , while powershell must be with admin rights. Do not forget to cancel the permission.

PPS Purely from innate gloating: a console application written in Delphi makes this conversion 7.5 times faster, specially measured :) And you don’t need to change the permissions: P.