Powershell select-string Matching x AND y - powershell

I'm trying to do a search of some log files for lines that contain certain strings. The files contain multiple lines like:
ALARM 11/08/2014 10:00:02,InFILE typeID,actionID,customerID: various_other_data_here
ALARM 11/08/2014 10:00:03,OutFILE typeID,actionID,customerID: various_other_data_here
I'm trying to find all lines in all files that have both 'ALARM' and 'OutFILE' in them.
I can use:
select-string .\*.log -pattern "ALARM"
to find all instances of 'ALARM', but how can I add the additional 'OutFILE'.
I've searched for this and found loads of examples that seem to be aimed at matching really copmplex strings with long ReEx, but nothing that is for matching simple x AND y type strings.

You can use a simple regex to match your scenario. This should work:
select-string .\*.log -pattern 'ALARM.*outFile'

Related

Select-String - find a string that spans multiple lines

I am trying to read lines in a file and search for a pattern that spans two lines. Looking at the file in notepad++ I see a LF char in the file.
Example log.txt:
I want to find this
value here: OK
My simple code does not work and returns nothing:
select-string -Path "log.txt" -Pattern "find this\n*value here: OK"
I have tried many combos of various things here including .+ and \r that I found posted on various threads. I can get the first line by using:
select-string -Path "log.txt" -Pattern "find this\n*"
Result of above is: I want to find this
Adding anything more to the line above results in nothing being returned. Any ideas how to do this using select-string? I was trying to avoid using get content due to the potential size of the files I am working with.
So I think I understand your question. If you have a file that has a line that you want to key off of then the next line is the line that you want to look at:
(Select-String -Path "Log.txt" -Pattern "find this" -Context 1).Context.PostContext
I wasn't sure if that carriage return was an artifact of your formatting or not. If it is not then this would work better:
(Select-String -Path "Log.txt" -Pattern "find this" -Context 2).Context.PostContext[1]
Here is a way to do it if you don't know how many lines will be between the two bits:
$file = Get-Content 'Log.txt' -Raw
$file -match '(?smi)I want to find this.*(value here: OK)'
$matches[1]
Since you might want a multi line regex solution you need to read in the text file as one string.
Using the test file:
Stfuf
Bagel
I want to find this
value here: OK
Things
I was able to get the result using a simple matching pattern that satisfies your example text.
(Get-Content -Raw c:\temp\test.txt | Select-String -Pattern "([\w ]+)\s*value here: OK").Matches.Groups[1].Value
regex101.com
Basically gets the text preceding invariant spaces, including newlines and the static text "value here: OK". Could be made better with positive look aheads but this seems to work fine.

Return True if two patterns are found in text with Powershell

This seems like a simple task but I can't seem to get it to work. I want to be able to read a text file and if two exact patterns are found return True. If it only finds one then it returns false.
I tried this, but it is not working:
Get-Content $log_path | Select-String -pattern "\bInstallation of\b|\bfailed\b" -AllMatches -quiet
What can I try next?
Your pattern contained a or | statement which basically says.
If one or the other (Installation or failed) is present, then it's a match.
What you want is to use a wildcard instead so both word and the in-between are part of the same match.
Select-String -pattern "\bInstallation .* failed\b" -quiet
Additional Notes
When having issues, there are online regex tester to allow you to test your regular expression.
I personally like a lot RegexHero even though you need IE because it is done in Silverlight because it has an Analyze section for your regular expression which breaks down your expression and gives an explanation in words of what you are doing.
Super useful when learning or just when dealing with always more complex epxressions.
For instance, your initial regular expression.
Although I did not personally used it, RegexStorm also looks promising and is not IE restricted.

select-string -pattern wildcard for a specific field

I'm still pretty new to powershell. Not sure to fix this or even what I am doing wrong.
My ultimate goal is to pull codes of 5 groups of 5 characters, groups of characters are delimited by -, from a long txt file. Example JTI45-534YS-PKQN6-MSE9S-2PFNM. There are multiple of these and I need to pull them all out of a file at once.
I'm trying multiple different variations on
Select-String .\reducedCodes.txt -Pattern "*-*-*-*-*"
or
Select-String .\reducedCodes.txt -Pattern "?????-?????-?????-?????-?????"
Thanks in advance.
Since your string that you are looking for looks like an alphanumeric, you could use a regex word, with is denoted by \w. And since there are five in a row, you could use \w{5} then they are separated by the -character. So Select-String normally gives you the lines containing the matches, and you just want the matches, you can then get the Matches property, where Value is the full match. Also note the Groups property where if you put the \w{5} inside () you could get an individual group.
(Select-String .\reducedCodes.txt -Pattern '\w{5}-\w{5}-\w{5}-\w{5}-\w{5}').Matches.Value

Read specific text from text files

I wrote a PowerShell script to compare two text files. In file1 the data is organised. But in file2 the data is not organised. I usually organize data manually. But now the data is increased. I need to automate organising using PowerShell.
PowerShell has to read data between two special characters. For example: <****# is my data. It has to read **** only. this pattern repeats 'n' number of times.
Use a regular expression <(.*?)# to match the relevant substring. .*? matches all characters between < and the next occurrence of # (non-greedy/shortest match). The parentheses put the match in a capturing group, so it can be referenced later.
Select-String -Path 'C:\path\to\file2.txt' -Pattern '<(.*?)#' -AllMatches |
Select-Object -Expand Matches |
ForEach-Object { $_.Groups[1].Value }
$_.Groups[1] refers to the first capturing group in a match.

Search two different string patterns in one line

THE SCENARIO
I have a *.txt file containing 3 lines:
test-1234.htm
test-5678.htm
somefile.htm
I need a script which will find specific string patterns in that *.txt file.
Currently, following script will find all *.htm files in *.txt file and store results in specified results.log file.
dir *.txt | Select-String -pattern "\.htm$" |Select-Object -Expandproperty line | Out-File results.log -Encoding utf8 -Width 500
QUESTION
How to modify it, so it only finds all "test-****.htm" lines?
(Will only log lines containing "test-" and ".htm")
Change the pattern argument to test-\d*.htm
'\d' - "Matches any digit character (0-9)"
'*' - "Match or more of the preceding token"
so it will match any num of digits
if you want to match at least 4 digits you can use test-\d{4,}.htm
i would recommend playing with regex using this site: http://regexr.com/
Thank you all for the hints!
Ultimately I got it working with:
test-.{4,}\.htm$
This way I was able to also include in results lines with digits+characters (for example "test-a12c.htm") and lines where there was something before the key word "test" (for example "this is a test-13bg.htm".
# user1432893
The site you've provided helped me with this! THX!
# Dave Sexton
with ^ in argument it didn't work.