Search code examples
csv

Editing csv files in batch


i need to edit this csv file in batch

id;category;name/code;description;sku;price;weight;options;enable discounts;discounts;availability type;available;pending;images
iqhk8mjh;Software;Quick Heal Antivirus Pro;Quick Heal Antivirus Pro;quickh1;29,90;0;;0;;Dynamic;10;0;C:\Users\Matteo\Dropbox\siti\quick\av2021.png
  • Delete column 2,3,4,5,7,8,9,10,11,13,14
  • change column title from availability to quantity
  • delete row with quantity=0
  • add column with title codice with static value for example 1234
  • then convert in txt (with /t separator) Is it possible?

i tryed with powershell


Solution

  • You can do it with PowerShell but I would recommend Miller (available here for several OSs) instead:

    mlr --icsv --ifs ';' --otsv cut -f 'id,price,available' then rename 'available,quantity' then filter '$quantity != 0' then put '$codice = 1234' file.csv
    

    Output:

    id        price  quantity  codice
    iqhk8mjh  29,90  10        1234
    
    Explanations
    • --icsv --ifs ';' => set the input format to CSV with ; as field separator.
    • --otsv => set the output format to TSV (TAB separated values).
    • cut -f 'id,price,available' => only keep those fields.
    • rename 'available,quantity' => rename the field available to quantity
    • filter '$quantity != 0' => only keep rows for which quantity isn't 0
    • put '$codice = 1234' => add the field codice with the value 1234 to each row

    note: then is for chaining operations in Miller