質問

I generally don't do much parsing however and am in need of some help. I have a plain text file that has the following format.

IP, Status, Server_Name, DNS_Name, Version, Authentication
IP, Status, Server_Name, DNS_Name, Version, Authentication
IP, Status, Server_Name, DNS_Name, Version, Authentication
                         etc..

Unfortunately when I try a simple copy and pasting onto a spreadsheet, for whatever reason I'm unable to properly paste the entire spreadsheet onto separate cells. Instead, whenever I paste I'm getting the entire spreadsheet put into one cell. I figured I could manage to do this using either python or powershell, either method works for me, what I'm having trouble with is implementation. I'd like to use the commas between each object as a method of signifying a new cell.

Currently trying to use Powershell I have the following that doesn't work (I don't mind doing this in python either).

$pattern = ','  
$content = Get-Content D:\Scripts\Temp\p.txt | Out-String
$content.Split($pattern) | Where-Object {$_ -match ','} | ForEach-Object {

$item = $_ -split "," | Where-Object {$_}

    New-Object PSobject -Property @{
        Name=$item[0].Split(',')[-1].Trim()
        Id = $item[1].Split(',')[-1].Trim()
        ResolutionPath=$item[2].Split(',')[-1].Trim()
        Endpoints=$item[4..($item.Count)]
    } | Select-Object Name,Id,ResolutionPath,Endpoints
}
役に立ちましたか?

解決

You can do this without code. Rename the extension to ".csv" and then open it in Excel.

You can also use the "Get External Data" option to customize the importing. I believe you can do this even without changing the file extension.

Give that a try.

他のヒント

You don't need to do any parsing, just use the Import-CSV cmdlet.

Import-CSV C:\path\to\yourfile.csv -Header "IP","STATUS","Server_Name","DNS_Name","Version","Authentication"

Find out more here: http://technet.microsoft.com/en-us/library/ee176874.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top