如何从表格格式的txt文件中导入数据并在PowerShell中导出为csv?

huangapple go评论110阅读模式
英文:

How to import data from table format txt file and export to csv in powershell?

问题

这是您的代码部分,已翻译好:

  1. $data = format-table 'E:\sample.txt' |
  2. Select-Object -Skip 2 |
  3. Out-String |
  4. ConvertFrom-StringData
  5. [PSCustomObject]$data | Export-Csv 'E:\out1.txt' -NoType
  6. $tempcsv = import-csv -path E:\out1.txt -Header
  7. "DATE","M","MA","S","FD","FU","PE","SS","AS","NO ","OO","CD" | select -skip 2
  8. $tempcsv | select DATE,M,MA, S ,FD,FU,PE,SS ,AS,NO,OO,CD| Group-Object M,MA,S,FD,FU|
  9. select Name,
  10. @{ Name = 'PE'; Expression = { ($_.Group | Measure-Object -Property PE -Sum).Sum } },
  11. @{ Name = 'SS'; Expression = { ($_.Group | Measure-Object -Property SS -Sum).Sum } },
  12. @{ Name = 'AS'; Expression = { ($_.Group | Measure-Object -Property AS -Sum).Sum } },
  13. @{ Name = 'NO'; Expression = { ($_.Group | Measure-Object -Property NO -Sum).Sum } },
  14. @{ Name = 'OO'; Expression = { ($_.Group | Measure-Object -Property OO -Sum).Sum } },
  15. @{ Name = 'CD'; Expression = { ($_.Group | Measure-Object -Property CD -Sum).Sum } }|
  16. EXPORT-CSV -path E:\FINAL_OUTPUT.txt
  17. Write-host "FINAL FILE conversion completed"

如果您需要任何进一步的帮助或解释,请告诉我。

英文:

i have sample.txt in table format

  1. Testing line1 in file
  2. DATE M MA S FD FU PE SS AS NO OO CD
  3. 05/31/23 FM 0 000 Account name 1 403 30.75 0.000 0.00 0 0 2
  4. 04/31/23 FM 0 000 Account name 2 403 30.75 0.000 0.00 0 0 2
  5. 03/31/23 FM 0 000 Account name 3 403 30.75 0.000 0.00 0 0 2

i want to convert this file into csv file and skip 2 rows and make changes like this by adding new column

FINAL_OUTPUT.csv

  1. "DATE","PE","SS","AS","NO","OO","CD","Name","data"
  2. "May","30.75","0.000","0.00","0","0","2","FM,0,000,Account name 1,403","data1"
  3. "April","30.75","0.000","0.00","0","0","2","FM,0,000,Account name 2,403","data1"
  4. "March","30.75","0.000","0.00","0","0","2","FM,0,000,Account name 3,403","data1"

so far i have tried this code

  1. $data = format-table 'E:\sample.txt' |
  2. Select-Object -Skip 2 |
  3. Out-String |
  4. ConvertFrom-StringData
  5. [PSCustomObject]$data | Export-Csv 'E:\out1.txt' -NoType
  6. $tempcsv=import-csv -path E:\out1.txt -Header
  7. "DATE”,”M”,”MA”,”S”,”FD”,”FU”,”PE”,”SS”,”AS”,”NO “,“OO”,”CD”| select -skip 2
  8. $tempcsv | select DATE,M,MA, S ,FD,FU,PE,SS ,AS,NO,OO,CD| Group-Object M,MA,S,FD,FU|
  9. select Name,
  10. @{ Name = 'PE'; Expression = { ($_.Group | Measure-Object -Property PE -Sum).Sum } },
  11. @{ Name = 'SS'; Expression = { ($_.Group | Measure-Object -Property SS -Sum).Sum } },
  12. @{ Name = 'AS'; Expression = { ($_.Group | Measure-Object -Property AS -Sum).Sum } },
  13. @{ Name = 'NO'; Expression = { ($_.Group | Measure-Object -Property NO -Sum).Sum } },
  14. @{ Name = 'OO'; Expression = { ($_.Group | Measure-Object -Property OO -Sum).Sum } },
  15. @{ Name = 'CD'; Expression = { ($_.Group | Measure-Object -Property CD -Sum).Sum } }|
  16. EXPORT-CSV -path E:\FINAL_OUTPUT.txt
  17. Write-host "FINAL FILE converSion completed"

答案1

得分: 1

以下是您要翻译的内容:

  1. <!-- language-all: sh -->
  2. Leaving the desired transformations and follow-up processing aside, here's how you can transform your input data to CSV:

Get-Content sample.txt | # Read file as plain text, line by line.
Select-Object -Skip 2 | # Skip first 2 lines.
ForEach-Object { $_ -replace '\s+', "t" } | # Replace whitespace runs w/ tab ConvertFrom-Csv -Delimiter "t" | # Read as tab-delimited
Select-Object DATE, PE, SS, AS, NO, OO, CD | # Select cols. of interest
ConvertTo-Csv # Convert to CSV (use Export-Csv to send to a file).

  1. Output (note that columns `Name` and `data` were omitted, as they're not in the original data):

"DATE","PE","SS","AS","NO","OO","CD"
"05/31/23","1","403","30.75","0.000","0.00","0"
"04/31/23","2","403","30.75","0.000","0.00","0"
"03/31/23","3","403","30.75","0.000","0.00","0"

  1. ---
  2. As for **what you tried**:
  3. * `Format-*` cmdlets emit output objects whose sole purpose is to provide _formatting instructions_ to PowerShell's for-display output-formatting system. In short: **only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_** - see [this answer](https://stackoverflow.com/a/55174715/45375) for more information.
  4. * [`ConvertFrom-StringData`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/ConvertFrom-StringData) is unsuited to parsing your data, because it expects strings with each line containing a single key-value pair in the format `key=value`
英文:

<!-- language-all: sh -->

Leaving the desired transformations and follow-up processing aside, here's how you can transform your input data to CSV:

  1. Get-Content sample.txt | # Read file as plain text, line by line.
  2. Select-Object -Skip 2 | # Skip first 2 lines.
  3. ForEach-Object { $_ -replace &#39;\s+&#39;, &quot;`t&quot; } | # Replace whitespace runs w/ tab
  4. ConvertFrom-Csv -Delimiter &quot;`t&quot; | # Read as tab-delimited
  5. Select-Object DATE, PE, SS, AS, NO, OO, CD | # Select cols. of interest
  6. ConvertTo-Csv # Convert to CSV (use Export-Csv to send to a file).

Output (note that columns Name and data were omitted, as they're not in the original data):

  1. &quot;DATE&quot;,&quot;PE&quot;,&quot;SS&quot;,&quot;AS&quot;,&quot;NO&quot;,&quot;OO&quot;,&quot;CD&quot;
  2. &quot;05/31/23&quot;,&quot;1&quot;,&quot;403&quot;,&quot;30.75&quot;,&quot;0.000&quot;,&quot;0.00&quot;,&quot;0&quot;
  3. &quot;04/31/23&quot;,&quot;2&quot;,&quot;403&quot;,&quot;30.75&quot;,&quot;0.000&quot;,&quot;0.00&quot;,&quot;0&quot;
  4. &quot;03/31/23&quot;,&quot;3&quot;,&quot;403&quot;,&quot;30.75&quot;,&quot;0.000&quot;,&quot;0.00&quot;,&quot;0&quot;

As for what you tried:

  • Format-* cmdlets emit output objects whose sole purpose is to provide formatting instructions to PowerShell's for-display output-formatting system. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing - see this answer for more information.

  • ConvertFrom-StringData is unsuited to parsing your data, because it expects strings with each line containing a single key-value pair in the format key=value

huangapple
  • 本文由 发表于 2023年6月15日 02:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定