2009-09-22 2 views
0

배경 : 여기서 목표는 select-string을 사용하여 powershell에서 몇 가지 기본 명령을 수행하는 것입니다. 어떤 이유로 든 예상대로 작동하지 않는 특정 사항이 있습니다. 이 출력은 powershell 기본 구문 질문에 대한 선택

$vfilter = 'c:/foo/bar/files/*.htm'; 
    Select-String -path $vfilter -pattern ".*DOCTY.*" | 
     sort LineNumber | 
     where-object { $_.Filename -match "02" } | 
     format-list | 
     out-file c:/00junk.txt; 

...

IgnoreCase : True 
    LineNumber : 1 
    Line  : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht 
       tp://www.w3.org/TR/html4/loose.dtd"> 
    Filename : 02junk.htm 
    Path  : C:\ ... \02junk.htm 
    Pattern : .*DOCTY.* 
    Context : 
    Matches : {<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h 
       ttp://www.w3.org/TR/html4/loose.dtd">} 

질문 :

1) 내가 배치에서 PowerShell을 방지 할 수있는 방법

은 다음 가정 "Line"및 "Matches"텍스트 속성 이 내용은 텍스트 파일로 전송되므로 콘솔 너비는 신경 쓰지 않으므로 텍스트 줄 바꿈이 발생하지 않도록하십시오.

2) 사용자 정의 출력을 여러 줄 문자열로 사용한다고 가정합니다. 루비 에서 (예를 들어) 나는 이런 식으로 그것을 할 수 :

custom_string = ''; 
    items.each{|myitem| 
     custom_string += %Q[ 
      ### begin output ### 
      HereIs::LineNumber! --> #{myitem['LineNumber']} 
      HereIs::Path!  --> #{myitem['Path']} 
      This is the actual line (below): 

      #{myitem['Line']} 
     ] 
    } 
    custom_string.tofile('c:/00junk.txt'); 

어떻게 동일하거나 유사한 일을 할 PowerShell을받을 수 있나요?

3) 위의 2)는 powershell로 어떻게 할 수 있습니까?

답변

8

1) 포장을 방지하려면 Out-File에 대해 거대한 width 매개 변수를 지정할 수 있습니다.

$vfilter = 'c:/foo/bar/files/*.htm'; 
Select-String -path $vfilter -pattern ".*DOCTY.*" | 
    sort LineNumber | 
    where-object { $_.Filename -match "02" } | 
    ForEach-Object { 
@" 
Line Number: $($_.LineNumber) 
Path:  $($_.Path) 
This is the actual line (below): 

$($_.Line) 

"@ 
    } | 
    out-file c:/00junk.txt; 
:

2) 사용 ForEach-Object (별칭 %는) 당신의 문자열을 생성합니다