Get Data from Log fine with CRLF

Hi all,

 

I am new to the forum and just need some assistance please. I am fairly familiar with Powershell and ok with Regular Expressions. I have a log file that I nee to get specific data out of, I have googled and have tried to play around with some things but don’t seem to be winning.

 

The log file looks like this:

 

================================================================================
User Name: Jane Doe

End of OP Order Status Report Time : 12:56:12 P
Number of orders selected : 1101
Total value (excluding VAT) : 234

================================================================================

================================================================================
User Name: John Doe

STK Stock File Update Date : 28/09/18 Time : 12:57:01
Stock code : STOCKITEM1
Record amended

 

What I am trying to get is only the content that starts with STK, I have managed to come up with the following regular expression:

(\STK(.)\s.\s.*)

 

Which according to Regex Storm.net does match what I am wanting. However, I don’t seem to be coming right with getting this information into a new file.

 

I am using the following script:

 

$input_path = ‘C:\Temp\testfile.txt’
$output_file = ‘C:\Temp\stk.txt’
$regex = ‘(\STK(.)\s.\s.*)’
select-string -Path $input_path -Pattern $regex -AllMatches | % { $.Matches} | % { $.Value } > $output_file

 

Which outputs the data to a new file, however it does not output all the information. The output of the file looks like this:

 

STK Stock File Update Date : 28/09/18 Time : 12:57:01
STK Stock File Update Date : 28/09/18 Time : 12:57:01
STK Stock File Update Date : 28/09/18 Time : 12:57:01

 

What I need is for it to look as follows:

STK Stock File Update Date : 28/09/18 Time : 12:57:01 Stock code : STOCKITEM1 Record amended

STK Stock File Update Date : 28/09/18 Time : 12:57:01 Stock code : STOCKITEM2 Record amended

etc. I suspect it has something to do with CRLF at the end of each line, which I can see using Notepad++.

I am sure it is something small.

Regards

 

 

 

Hi AndrewDowns97,
and welcome to the Powershell.org forum.

Please try to format your code as code here in the forum. When you post errors or sample data you should format that as code as well. That will prevent unwanted line breaks and keeps the original “format”. Thanks.

When you take a look at the output the cmdlet Select-String produces you’ll might notice that there are more information than just the match.

Select-String -Path $input_path -Pattern $regex -AllMatches | Format-List
If you want to output the following lines after the matched text you can use the -Context parameter to include it in the output. Then you just have to “extract” what you’re after. … like this:
Select-String -Path $input_path -Pattern regex -AllMatches -Context 2 | 
    ForEach-Object {
        .Line
$
.Context.PostContext
}
If you really wnat to have it on one line you have to concatenate the output … like this:
Select-String -Path $input_path -Pattern regex -AllMatches -Context 2 | 
    ForEach-Object {
        .Line, ($.Context.PostContext -join ’ ') -join ’ ’
}

One more thing: If you could please avoid posting that much white space next time I would be really thankful. :wink:

Why not just do this…

Clear-Host
[regex]::Matches('STK Stock File Update Date : 28/09/18 Time : 12:57:01
Stock code : STOCKITEM1
Record amended','^STK.* |Stock.* |Record.*').Value -join ' '
 
# Results

STK Stock File Update Date : 28/09/18 Time :  Stock code :  Record amended

As you can see there are extra spaces in your string as these do not show up in the post view due to post formatting.

Hmmm … I guess I need new glasses … where is the time “12:57:01” in the output and the “STOCKITEM1”? :wink: :smiley: :stuck_out_tongue:

Nah! Olaf, it’s not there. I’m very old, and well, you know, OPP (old people problems) kicks in without notice.
well, that and spaces…

Fixed version

Clear-Host
[regex]::Matches('STK Stock File Update Date : 28/09/18 Time : 12:57:01
Stock code : STOCKITEM1
Record amended','^STK.*|Stock.*|Record.*').Value -join ' '

# Results

# STK Stock File Update Date : 28/09/18 Time : 12:57:01 Stock code : STOCKITEM1 Record amended