I have a txt file that I need to convert to a csv file.<\/p>\n
The raw data looks like this:<\/p>\n
Date Field1 Field2 \n11/01 12,432.98 901,459<\/p>\n
There are a lot of blank spaces and no separator. I have tried various import-csv|export-csv combinations and just cannot get this right.<\/p>\n
The closest I have gotten is this:<\/p>\n
Import-Csv .\\ASK_ConsumerStats.txt -Delimiter ' ' | Export-csv ask.csv -NoType\n<\/code><\/pre>","upvoteCount":3,"answerCount":10,"datePublished":"2018-11-28T14:25:47.000Z","author":{"@type":"Person","name":"danhoffmann","url":"https://community.spiceworks.com/u/danhoffmann"},"suggestedAnswer":[{"@type":"Answer","text":"I have a txt file that I need to convert to a csv file.<\/p>\n
The raw data looks like this:<\/p>\n
Date Field1 Field2 \n11/01 12,432.98 901,459<\/p>\n
There are a lot of blank spaces and no separator. I have tried various import-csv|export-csv combinations and just cannot get this right.<\/p>\n
The closest I have gotten is this:<\/p>\n
Import-Csv .\\ASK_ConsumerStats.txt -Delimiter ' ' | Export-csv ask.csv -NoType\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2018-11-28T14:25:47.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/1","author":{"@type":"Person","name":"danhoffmann","url":"https://community.spiceworks.com/u/danhoffmann"}},{"@type":"Answer","text":"Do a replace on the file for \" \" to “,”<\/p>","upvoteCount":2,"datePublished":"2018-11-28T14:29:37.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/2","author":{"@type":"Person","name":"rockn","url":"https://community.spiceworks.com/u/rockn"}},{"@type":"Answer","text":"
Are they spaces or tabs? If you try this do you get a nice PowerShell list?<\/p>\n
Import-Csv .\\ASK_ConsumerStats.txt -Delimiter \"`t\"\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2018-11-28T14:36:25.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/3","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Martin9700:<\/div>\n
\nAre they spaces or tabs? If you try this do you get a nice PowerShell list?<\/p>\n
Import-Csv .\\ASK_ConsumerStats.txt -Delimiter \"`t\"\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nYes I do<\/p>","upvoteCount":1,"datePublished":"2018-11-28T14:38:36.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/4","author":{"@type":"Person","name":"danhoffmann","url":"https://community.spiceworks.com/u/danhoffmann"}},{"@type":"Answer","text":"
Are the gaps between each piece of data consistent? There’s a really neat regex way to do this, I’m sure, but since I’m still a regex newbie, the way I would normally handle this would be to open the document in notepad and tell it to replace each two space character pair with a single character that you want to use as your delimiter. It looks like your data already has commas in it. So, commas wouldn’t work. Then, tell notepad to replace each pair of delimiting characters with a single delimiting character until each gape between data pieces gets reduced to a single delimiter separation. Like so:<\/p>\n
Data1 Data2 (5 space character separation)<\/p>\n
Tell notepad to replace each pair of space characters with “&” changes it to:<\/p>\n
Data1&& Data2 (two & and a space)<\/p>\n
Tell notepad to replace each “&” and a single space character with just “&” changes it to:<\/p>\n
Data1&&Data2<\/p>\n
Tell notepad to change each “&&” to “&” to change to the final form of:<\/p>\n
Data1&Data2<\/p>\n
Then, use your special character as you delimiter.<\/p>\n
Elegant? Not at all. Simple? Yes. Something to keep doing if you need to do this frequently? Not in the least bit.<\/p>","upvoteCount":0,"datePublished":"2018-11-28T14:39:06.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/5","author":{"@type":"Person","name":"jjoyner1985","url":"https://community.spiceworks.com/u/jjoyner1985"}},{"@type":"Answer","text":"
Date Consume Consumer G\n---------------------------------------------------------------------\n11/01 31,541.95 901,459\n11/02 96,337.36 901,459\n11/03 161,880.77 901,459\n11/05 176,760.41 901,459\n11/06 197,560.41 901,459\n<\/code><\/pre>\nbut still lots of spaces<\/p>","upvoteCount":0,"datePublished":"2018-11-28T14:39:20.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/6","author":{"@type":"Person","name":"danhoffmann","url":"https://community.spiceworks.com/u/danhoffmann"}},{"@type":"Answer","text":"
Have you tried using a tab delimter? That would be something like this:<\/p>\n
Import-Csv .\\ASK_ConsumerStats.txt -Delimiter “`t” | Export-csv ask.csv -NoType<\/p>\n
Please note that the second character in the “'t” string is not a single quote mark, it is a backtick.<\/p>","upvoteCount":1,"datePublished":"2018-11-28T14:39:43.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/7","author":{"@type":"Person","name":"reedsmoller1451","url":"https://community.spiceworks.com/u/reedsmoller1451"}},{"@type":"Answer","text":"\n\n
<\/div>\n
highwireact:<\/div>\n
\nHave you tried using a tab delimter? That would be something like this:<\/p>\n
Import-Csv .\\ASK_ConsumerStats.txt -Delimiter “`t” | Export-csv ask.csv -NoType<\/p>\n
Please note that the second character in the “'t” string is not a single quote mark, it is a backtick.<\/p>\n<\/blockquote>\n<\/aside>\n
This is where I was going. Since it’s tab delimited we can import it as PowerShell objects, then export them back out.<\/p>","upvoteCount":0,"datePublished":"2018-11-28T14:43:41.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/8","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"
I got it to work…not via powershell, but it’s simple and it works for me.<\/p>\n
FYI - its a crystal report, so I put all my output in a text field and hard coded the quotes and separators<\/p>\n
“field1”,“field2”,“field3”<\/p>\n
and before you ask, I am not able to export the crystal as csv due to other complications<\/p>\n
Thanks for the super fast feedback from everyone!!<\/p>","upvoteCount":0,"datePublished":"2018-11-28T14:51:46.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/9","author":{"@type":"Person","name":"danhoffmann","url":"https://community.spiceworks.com/u/danhoffmann"}},{"@type":"Answer","text":"
You can do something like this:<\/p>\n
$Text = Get-Content Drive:\\Path\\File\n$Text -ireplace '\\s+',';' | Out-File Drive:\\Path\\File.csv\n$CSV = Import-Csv Drive:\\Path\\File.csv -Delimiter ';'\n<\/code><\/pre>\nI used a semicolon ( as a delimiter, because your data contains commas.<\/p>","upvoteCount":0,"datePublished":"2018-11-28T15:15:39.000Z","url":"https://community.spiceworks.com/t/powershell-convert-to-csv/685877/10","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}}]}}
I have a txt file that I need to convert to a csv file.
The raw data looks like this:
Date Field1 Field2
11/01 12,432.98 901,459
There are a lot of blank spaces and no separator. I have tried various import-csv|export-csv combinations and just cannot get this right.
The closest I have gotten is this:
Import-Csv .\ASK_ConsumerStats.txt -Delimiter ' ' | Export-csv ask.csv -NoType
3 Spice ups
rockn
(Rockn)
November 28, 2018, 2:29pm
2
Do a replace on the file for " " to “,”
2 Spice ups
martin9700
(Martin9700)
November 28, 2018, 2:36pm
3
Are they spaces or tabs? If you try this do you get a nice PowerShell list?
Import-Csv .\ASK_ConsumerStats.txt -Delimiter "`t"
3 Spice ups
Are the gaps between each piece of data consistent? There’s a really neat regex way to do this, I’m sure, but since I’m still a regex newbie, the way I would normally handle this would be to open the document in notepad and tell it to replace each two space character pair with a single character that you want to use as your delimiter. It looks like your data already has commas in it. So, commas wouldn’t work. Then, tell notepad to replace each pair of delimiting characters with a single delimiting character until each gape between data pieces gets reduced to a single delimiter separation. Like so:
Data1 Data2 (5 space character separation)
Tell notepad to replace each pair of space characters with “&” changes it to:
Data1&& Data2 (two & and a space)
Tell notepad to replace each “&” and a single space character with just “&” changes it to:
Data1&&Data2
Tell notepad to change each “&&” to “&” to change to the final form of:
Data1&Data2
Then, use your special character as you delimiter.
Elegant? Not at all. Simple? Yes. Something to keep doing if you need to do this frequently? Not in the least bit.
Date Consume Consumer G
---------------------------------------------------------------------
11/01 31,541.95 901,459
11/02 96,337.36 901,459
11/03 161,880.77 901,459
11/05 176,760.41 901,459
11/06 197,560.41 901,459
but still lots of spaces
Have you tried using a tab delimter? That would be something like this:
Import-Csv .\ASK_ConsumerStats.txt -Delimiter “`t” | Export-csv ask.csv -NoType
Please note that the second character in the “'t” string is not a single quote mark, it is a backtick.
1 Spice up
martin9700
(Martin9700)
November 28, 2018, 2:43pm
8
highwireact:
Have you tried using a tab delimter? That would be something like this:
Import-Csv .\ASK_ConsumerStats.txt -Delimiter “`t” | Export-csv ask.csv -NoType
Please note that the second character in the “'t” string is not a single quote mark, it is a backtick.
This is where I was going. Since it’s tab delimited we can import it as PowerShell objects, then export them back out.
I got it to work…not via powershell, but it’s simple and it works for me.
FYI - its a crystal report, so I put all my output in a text field and hard coded the quotes and separators
“field1”,“field2”,“field3”
and before you ask, I am not able to export the crystal as csv due to other complications
Thanks for the super fast feedback from everyone!!
Evan7191
(Evan7191)
November 28, 2018, 3:15pm
10
You can do something like this:
$Text = Get-Content Drive:\Path\File
$Text -ireplace '\s+',';' | Out-File Drive:\Path\File.csv
$CSV = Import-Csv Drive:\Path\File.csv -Delimiter ';'
I used a semicolon ( as a delimiter, because your data contains commas.