2016-05-17 9 views

答えて

1

これはノート*

$filecontents = @' 
test|wdthe$muce 

check|muce6um#% 
'@.split("`n") 

# instead of the above, you would use this with the path of the file 
# $filecontents = get-content 'c:\temp\file.txt' 

$hash = @{} 
$filecontents | ? {$_ -notmatch '^(?:\s+)?$'} | % { 
    $split = $_.Split('|') 
    $hash.Add($split[0], $split[1]) 
} 
$result = [pscustomobject]$hash 

$result 

# and to get just what is inside 'test' 
$result.test 

一つの可能​​な解決策になることがあります。一つだけ各行のがファイル内に存在している場合にのみ動作する可能性があります。あなたがエラーを取得した場合、おかげ

$search = 'test' 
$filecontents | ? {$_ -match "^$search\|"} | % { 
    $_.split('|')[1] 
} 
+0

この他の方法を試してみてください!!それは完璧に..働いた! – Cooluser

+0

変数[email protected]があり、 "@"の前にテキストを分割してファイルを検索する方法を考えます。ありがとう...! – Cooluser

+0

'$ prop = $ from.split( '@')[0]; $ result '$ prop' –

0

まず、ファイルからテキストを読み取る必要があります。

$content = Get-Content "c:\temp\myfile.txt" 

次に、一致する各行のポストパイプ部分をグラブします。

$postPipePortion = $content | Foreach-Object {$_.Substring($_.IndexOf("|") + 1)} 

そして、それは代わりに変数を使用してのあなたも一緒にそれをデイジーチェーン接続できPowerShellのだから:

Get-Content "C:\temp\myfile.txt" | Foreach-Object {$_.Substring($_.IndexOf("|") + 1)} 

上記では、すべての行が|文字が含まれています知っていることが起こることを想定しています。そうでない場合、あなたはこのように、文字を持っているだけの行を選択する必要があります。

Get-Content "C:\temp\myfile.txt" | Select-String "|" | Foreach-Object {$_.Line.Substring($_.Line.IndexOf("|") + 1)} 

(あなたが選択し、文字列のMatchInfoオブジェクトを返すためだけではなく$_今の$_.Lineを使用する必要があります文字列ではなく)

希望に役立ちます。がんばろう。

0
gc input.txt |? {$_ -match '^test'} |% { $_.split('|') | select -Index 1 } 

or 

sls '^test' -Path input.txt |% { $_.Line.Split('|') | select -Index 1 } 

or 

sls '^test' input.txt |% { $_ -split '\|' | select -Ind 1 } 

or 

(gc input.txt).Where{$_ -match '^test'} -replace '.*\|' 

or 

# Borrowing @Anthony Stringer's answer shape, but different 
# code, and guessing names for what you're doing: 

$users = @{} 
Get-Content .\input.txt | ForEach { 
    if ($_ -match "(?<user>.*)\|(?<pwd>.*)") { 
     $users[$matches.user]=$matches.pwd 
    } 
} 
$users = [pscustomobject]$users 
関連する問題