Outputting data separated by tabs to text file

戏子无情 提交于 2019-12-20 05:40:59

问题


I have a bunch of xml files, in each of these files there could be multiple variants (different language locale codes en-us, en-ca, en-gb etc.) I am new to powershell so I'm probably not understanding several concepts.

I'm able to pull all this information but I'm having trouble outputting it in the format I want. I have tried using Out-File and have looked into Export-Csv but I'm just not able to get it to do what I want. Any help would be greatly appreciated.

This is the format I want out (the content will be copied into Excel)

File Path  |  ar-ae  |  ar-sa  |  cs-cz  |  da-dk
File 1         X        X                 X
File 2         X        X        X        X
File 3                  X         

So basically I want a table with the headers being File Path and each language code. Then under that I want to list all the files and put an X under each locale that exists in the file.

This is my code

if (Test-Path ".\siteIA.txt") {
Clear-Content ".\siteIA.txt"
}

$locales = @(
"ar-ae"
"ar-sa"
"cs-cz"
"da-dk"
"de-at"
"de-ch"
"de-de"
"el-gr"
"en-au"
"en-ca"
"en-gb"
"en-hk"
"en-ie"
"en-in"
"en-nz"
"en-sg"
"en-us"
"en-za"
"es-ar"
"es-cl"
"es-co"
"es-es"
"es-mx"
"fi-fi"
"fr-be"
"fr-ca"
"fr-ch"
"fr-fr"
"he-il"
"hu-hu"
"it-it"
"ja-jp"
"ko-kr"
"nb-no"
"nl-be"
"nl-nl"
"pl-pl"
"pt-br"
"pt-pt"
"ru-ru"
"sk-sk"
"sv-se"
"tr-tr"
"zh-hk"
"zh-tw"
)

$locales = $locales | sort

$header = "Path`t" + $locales

Get-ChildItem C:\Users\tests *.xml -recurse | 
% { 
    $outArray = @()
    $file = [xml](Get-Content $_.fullname)

    $path = $file.ExportedContentItem.path
    $name = $file.ExportedContentItem.name
    $availableLocales=@()

    $out = $path + "/" + $name + "`t"

    $file.ExportedContentItem.ContentItem.Variant | % { 
        $availableLocales += $_.variantCulture
    }

    $availableLocales = $availableLocales | sort

    foreach ($locale in $locales){
        if ($availableLocales -contains $locale){
            $out = "X"
            Add-Content ".\siteIA.txt" "X`t"
        } else {
            $out = ""
            Add-Content ".\siteIA.txt" "`t"
        }       
    }

    Add-Content ".\siteIA.txt" "T"
    Add-Content ".\siteIA.txt" "E"
    Add-Content ".\siteIA.txt" "S"

}

回答1:


If you want to import the output into Excel later, it's best to simply create a TSV (Tab Separated Values) file. The Export-Csv cmdlet can do that for you.

Create custom objects with File Path and the locales as properties, and set the all those properties whose names are listed in the Variant node to X. Then export the objects using a tab as delimiter:

Get-ChildItem C:\Users\tests *.xml -recurse | % {
  $contentItem = [xml](Get-Content $_.FullName).ExportedContentItem

  $o = New-Object -Type PSObject -Property @{
    'File Path' = Join-Path $contentItem.Path $contentItem.Name
    'ar-ae'     = ''
    'ar-sa'     = ''
    ...
    'zh-hk'     = ''
    'zh-tw'     = ''
  }

  $contentItem.ContentItem.Variant | % {
    $o."$($_.variantCulture)" = "X"
  }

  $o
} | Export-Csv 'C:\path\to\output.txt' -NoType -Delimiter "`t"


来源:https://stackoverflow.com/questions/22352370/outputting-data-separated-by-tabs-to-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!