Solved

Convert CSV

Posted on 2013-05-18
9
426 Views
Last Modified: 2013-05-18
Hi,

i've a csv it looks like that:

UserId;OrderIdent;OrderID;Action;Product;Version
Z528517;2397277;ZP145676-001;YES;MAIL;MSC
Z528516;2397278;ZP145676-002;YES;MAIL;MSC
Z528515;2397279;ZP145676-003;YES;MAIL;MSC
Z528514;2397281;ZP145676-004;YES;MAIL;MS2003
Z528484;2397284;ZP145677-001;YES;MAIL;MSC
Z528474;2397285;ZP145677-002;YES;MAIL;MSC
Z528396;2397345;ZP145687-003;YES;MAIL;MS2003
Z130561;2397436;ZP145706-001;YES;MAIL;MSC
Z527465;2397632;ZP145766-001;YES;AdA
Z527465;2397633;ZP145766-002;YES;MAIL;MSXC2010
Z831113;2397637;ZP145767-001;YES;AdA
Z831113;2397638;ZP145767-002;YES;MAIL

Open in new window


I want to convert and sort it to this form (The last lines should be "AdA" and "MS2003"
to use it for some automatic exchange and AD coding



UserId,OrderIdent,OrderID,Action,Product,Version
Z528517,2397277,ZP145676-001,YES,MAIL,MSC
Z528516,2397278,ZP145676-002,YES,MAIL,MSC
Z528515,2397279,ZP145676-003,YES,MAIL,MSC
Z528484,2397284,ZP145677-001,YES,MAIL,MSC
Z528474,2397285,ZP145677-002,YES,MAIL,MSC
Z130561,2397436,ZP145706-001,YES,MAIL,MSC
Z527465,2397633,ZP145766-002,YES,MAIL,MSC
Z831113,2397638,ZP145767-002,YES,MAIL,MSC
Z528514,2397281,ZP145676-004,YES,MAIL,MS2003
Z528396,2397345,ZP145687-003,YES,MAIL,MS2003
Z527465,2397632,ZP145766-001,YES,AdA
Z831113,2397637,ZP145767-001,YES,AdA

Open in new window


Who can help here?

Thank you so much
0
Comment
Question by:Mandy_
  • 4
  • 3
  • 2
9 Comments
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39177054
Try the following:
import-csv H:\filecsv.csv | Sort-Object version -Descending | ft -AutoSize

your source data does not match with the resultant data what you require  as per your question.

Or try: import-csv H:\filecsv.csv | Sort-Object Product, version -Descending | ft -AutoSize
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39177055
import-csv H:\filecsv.csv | Sort-Object version -Descending | ft -AutoSize | export-csv H:\ filecsv2.csv
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39177067
The above code works if you replace all the ; with commas
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39177311
marahman3001,

don't use format-* if exporting to CSV. The format cmdlets convert anything into a single string per line, destroying object structure and properties.
And *-csv has a delimiter parameter to tell which one to use ;-).


Mandy_,

I'm not certain about the proper order of rows you demand, but I would go with
(import-csv -delimter ';' file.csv | sort product, version -descending |
   converto-csv -notype -delimiter ';') -replace '"' | out-file result.csv

Open in new window

export-csv and convertto-csv add double quotes to each field, so we have to remove those to keep the original format.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Author Comment

by:Mandy_
ID: 39177321
hi,

thanks. sorry so far not working for me

What exactly did  ft -AutoSize
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39177333
It's short form for format-table
Autosize displays the data in columns with automatic width according to the data

I guess your input data sample is not matching with the output you require. There must be error in typo etc.
Eg. Msxc2010  it's not in your output
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39177368
Mandy_,

If something does not work for you, please tell us what did not work - e.g. list the expected result and what you got instead. And it is always a good idea to mention which post you refer to.
0
 
LVL 2

Author Comment

by:Mandy_
ID: 39177486
Thank you for your help. this one below is working fine.


(import-csv -delimter ';' file.csv | sort product, version -descending |
   converto-csv -notype -delimiter ';') -replace '"' | out-file result.csv
0
 
LVL 2

Author Closing Comment

by:Mandy_
ID: 39177487
very good . thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now