phdit
asked on
CSV file manipulation
Hi All,
I have a csv file wich looks like the below with many thousands of lines.
0021072204,21110016,\\89.1 .1.133\Sea rchByMail\ H0517\H051 7_CB668_65 167\000000 18.pdf
I need a Powershell script to go through each line and when ever the first colum character count equals 10 like in the example above swop around colum 1 and 2 so the above example becomes
21110016,0021072204,\\89.1 .1.133\Sea rchByMail\ H0517\H051 7_CB668_65 167\000000 18.pdf
Thanks
John Harris
I have a csv file wich looks like the below with many thousands of lines.
0021072204,21110016,\\89.1
I need a Powershell script to go through each line and when ever the first colum character count equals 10 like in the example above swop around colum 1 and 2 so the above example becomes
21110016,0021072204,\\89.1
Thanks
John Harris
ASKER
Hi thanks for that.
No the csv does not have any headers where would I add
If your csv doesn't have a header line, just add
-Header Col1, Col2, Col3
I actually got a bit ahead of myself its not a csv file its a text file with , differencing the fields it gets converted to a csv later then loaded into sql.
No the csv does not have any headers where would I add
If your csv doesn't have a header line, just add
-Header Col1, Col2, Col3
I actually got a bit ahead of myself its not a csv file its a text file with , differencing the fields it gets converted to a csv later then loaded into sql.
OK then; this will add and remove the header line:
Import-Csv -Path C:\Temp\input.csv -Header Col1, Col2, Col3 |
ForEach-Object {If ($_.Col1.Length -eq 10) {$_.Col1, $_.Col2 = $_.Col2, $_.Col1}; $_} |
ConvertTo-Csv -NoTypeInformation |
Select-Object -Skip 1 |
Set-Content -Path | C:\Temp\output.csv
Do you mind the quotes around the fields?
ASKER
PS C:\Users\scanner\Documents > C:\Users\scanner\Documents \hag.ps1
C:\Temp\output.csv : The term 'C:\Temp\output.csv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\scanner\Documents \hag.ps1:5 char:22
+ Set-Content -Path | C:\Temp\output.csv
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Temp\output.csv:String ) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Im getting this error now running your second script I also DO need the quotes removing too pls.
C:\Temp\output.csv : The term 'C:\Temp\output.csv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\scanner\Documents
+ Set-Content -Path | C:\Temp\output.csv
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Temp\output.csv:String
+ FullyQualifiedErrorId : CommandNotFoundException
Im getting this error now running your second script I also DO need the quotes removing too pls.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much that works a treat.
If your csv doesn't have a header line, just add
-Header Col1, Col2, Col3
as argument for Import-Csv
Open in new window