简体   繁体   English

如何使用 powershell 将此文本文件 output 转换为表格格式?

[英]How do I output this Text file into a table format using powershell?

Here is my.txt file:这是 my.txt 文件:

    CH: "g@email.org", "j@email.org" 
    DL: "y@email.org", "ri@email.org" 
    KM: "b@email.org", "n@email.org" 
    CE: "tap@email.org", "k@email.org" 
    DE: "pi@email.org", "sp@email.org" 
    KL: "reedc@email.org", "be@email.org" 
    PR: "ks@email.org", "bt@email.org" 
    EC: "brn@email.org" 

I want the output to look something like this:我希望 output 看起来像这样:

     Building | Email
     CH:       g@email.com

I'm pretty new to powershell please help我对 powershell 很陌生,请帮忙

This is pretty easy to solve.这很容易解决。

So, let's assume that input is in a text file called 'Buildings.txt'.所以,让我们假设输入在一个名为“Buildings.txt”的文本文件中。 We can load it into memory like so:我们可以像这样将它加载到 memory 中:

$textFile = Get-Content .\Buildings.txt

Next up, we can split this file into lines by calling the .Split() method and giving it the overload of newline, which looks like this:接下来,我们可以通过调用.Split()方法并为其赋予换行符的重载,将这个文件分成几行,如下所示:

$Lines = $textFile.Split("`n")
$lines.Count
>8

Then we can simply step through each line and then split those on the colon character, : and finally, build up an object to send to the screen or put in a file or whathave you.然后我们可以简单地遍历每一行,然后在冒号字符上拆分它们, :最后,构建一个 object 发送到屏幕或放入文件或你有什么。

ForEach($l in $Lines){
    $building = $l.Split(":")[0].Trim()
    $email    = $l.Split(":")[1].Trim()
    [pscustomObject]@{
        Building = $building;
        Email = $email
    }
}

Running that gives us this, which is a useful format for further automation:运行它给了我们这个,这是进一步自动化的有用格式:

Building Email                            
-------- -----                            
CH       "g@email.org", "j@email.org"     
DL       "y@email.org", "ri@email.org"    
KM       "b@email.org", "n@email.org"     
CE       "tap@email.org", "k@email.org"   
DE       "pi@email.org", "sp@email.org"   
KL       "reedc@email.org", "be@email.org"
PR       "ks@email.org", "bt@email.org"   
EC       "brn@email.org"                  

This should get you going in the right direction.这应该让你朝着正确的方向前进。 If not, leave a comment:)如果没有,请发表评论:)

I am not really clear on what you are after.我不太清楚你在追求什么。

If this is your data, it is already a table, it's a formatting effort.如果这是您的数据,它已经是一个表格,这是一个格式化工作。

'
CH: "g@email.org", "j@email.org" 
DL: "y@email.org", "ri@email.org" 
KM: "b@email.org", "n@email.org" 
CE: "tap@email.org", "k@email.org" 
DE: "pi@email.org", "sp@email.org" 
KL: "reedc@email.org", "be@email.org" 
PR: "ks@email.org", "bt@email.org" 
EC: "brn@email.org"
' | 
Out-File -FilePath 'D:\Temp\EmailData.txt'

Get-Content -Path 'D:\Temp\EmailData.txt'
<#
# Results

CH: "g@email.org", "j@email.org" 
DL: "y@email.org", "ri@email.org" 
KM: "b@email.org", "n@email.org" 
CE: "tap@email.org", "k@email.org" 
DE: "pi@email.org", "sp@email.org" 
KL: "reedc@email.org", "be@email.org" 
PR: "ks@email.org", "bt@email.org" 
EC: "brn@email.org
#>

You can just read this back as a table...您可以将其作为表格读回...

Import-Csv -Path 'D:\Temp\EmailData.txt' -Delimiter ':' -Header Building, Email 
<#
# Results

Building Email                           
-------- -----                           
CH       g@email.org, "j@email.org"      
DL       y@email.org, "ri@email.org"     
KM       b@email.org, "n@email.org"      
CE       tap@email.org, "k@email.org"    
DE       pi@email.org, "sp@email.org"    
KL       reedc@email.org, "be@email.org" 
PR       ks@email.org, "bt@email.org"    
EC       brn@email.org  
#>

... and output it back out. ...和 output 退出。 Yet, that is a bit redundant然而,这有点多余

Import-Csv -Path 'D:\Temp\EmailData.txt' -Delimiter ':' -Header Building, Email | 
Export-Csv -Path 'D:\Temp\EmailData.csv' -NoTypeInformation

View the raw data查看原始数据

Get-Content -Path 'D:\Temp\EmailData.csv'
<#
# Results

"Building","Email"
"CH","g@email.org, ""j@email.org"" "
"DL","y@email.org, ""ri@email.org"" "
"KM","b@email.org, ""n@email.org"" "
"CE","tap@email.org, ""k@email.org"" "
"DE","pi@email.org, ""sp@email.org"" "
"KL","reedc@email.org, ""be@email.org"" "
"PR","ks@email.org, ""bt@email.org"" "
"EC","brn@email.org" 
#>

See the table view查看表格视图

Import-Csv -Path 'D:\Temp\EmailData.csv'
<#
# Results

Building Email                           
-------- -----                           
CH       g@email.org, "j@email.org"      
DL       y@email.org, "ri@email.org"     
KM       b@email.org, "n@email.org"      
CE       tap@email.org, "k@email.org"    
DE       pi@email.org, "sp@email.org"    
KL       reedc@email.org, "be@email.org" 
PR       ks@email.org, "bt@email.org"    
EC       brn@email.org
#>

If you are saying, you want the above look in a file, for say printing later, you can just output it that way.如果你说,你想在一个文件中查看上面的内容,以便稍后打印,你可以这样 output 。

Import-Csv -Path 'D:\Temp\EmailData.csv' | 
Out-File -FilePath 'D:\Temp\EmailData.dat'

Get-Content -Path 'D:\Temp\EmailData.dat'
<#
# Results

Building Email                           
-------- -----                           
CH       g@email.org, "j@email.org"      
DL       y@email.org, "ri@email.org"     
KM       b@email.org, "n@email.org"      
CE       tap@email.org, "k@email.org"    
DE       pi@email.org, "sp@email.org"    
KL       reedc@email.org, "be@email.org" 
PR       ks@email.org, "bt@email.org"    
EC       brn@email.org
#>

The file extensions is really not important.文件扩展名真的不重要。 I just did that to show I am working with all the same stuff, the same way.我这样做只是为了表明我正在以同样的方式处理所有相同的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM