xbath
xbath

Reputation: 21

Each string in new line

I have following powershell code which gain several strings from html. These strings are in one line, but I want each string on new line. What I have so far is this

#find appropriate date
$najit = select-string -Path C:\pokus\output.html -Pattern 'kr\w{2}-\d{4}-\d{2}-\d{2}' -AllMatches | % { $_.Matches } | % { $_.Value } 

# write output to file
Write-Host $najit *>> C:\pokus\najit.txt

Upvotes: 0

Views: 37

Answers (1)

Adam Stotler
Adam Stotler

Reputation: 81

It appears that you are having an issue writing the output. When I tested the pattern on a .html file, they were all on separate lines. You need to remove the write-host to output to a file.

#find appropriate date 
$najit = select-string -Path C:\pokus\output.html -Pattern 'kr\w{2}-\d{4}-\d{2}-\d{2}' -AllMatches | % { $_.Matches } | % { $_.Value } 

#output to file
$najit *>> C:\pokus\najit.txt

# or the powershell way
$najit | Out-File C:\pokus\najit.txt -Append

If you are not looping this in a foreach, then the append may be unnecessary. In that case, It should look like this:

#output to file
$najit > C:\pokus\najit.txt

# or the powershell way
$najit | Out-File C:\pokus\najit.txt 

It can also be done on one line as so:

#find appropriate date & output to file
$najit = select-string -Path C:\pokus\output.html -Pattern 'kr\w{2}-\d{4}-\d{2}-\d{2}' -AllMatches | % { $_.Matches } | % { $_.Value } | Out-file C:\pokus\najit.txt

Upvotes: 1

Related Questions