将制表符分隔的数据输出到文本文件 [英] Outputting data separated by tabs to text file
问题描述
我有一堆xml文件,在这些文件中的每一个中都可能有多个变体(en-us,en-ca,en-gb等不同的语言区域设置代码),我是Powershell的新手,所以我可能不了解几个概念.
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.
我能够提取所有这些信息,但是我无法以所需的格式输出它.我已经尝试过使用Out-File
并研究了Export-Csv
,但是我无法让它做我想做的事情.任何帮助将不胜感激.
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.
这是我想要的格式(内容将被复制到Excel中)
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
所以基本上我想要一个表头为文件路径和每种语言代码的表.然后,在此之下,我要列出所有文件,并在文件中存在的每个语言环境下放置一个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.
这是我的代码
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"
}
推荐答案
如果以后要将输出导入Excel,最好只创建一个TSV(制表符分隔值)文件. Export-Csv
cmdlet可以为您做到这一点.
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.
使用File Path
和语言环境作为属性创建自定义对象,并将名称在Variant
节点中列出的所有那些属性设置为X
.然后使用制表符作为分隔符导出对象:
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"
这篇关于将制表符分隔的数据输出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!