Get string values after a pattern using PowerShell - powershell

I want to get the string which are there after $ctrl in below html code snippet:
<div ng-if="$ctrl.CvReportModel.IsReady">
ng-click="$ctrl.confirmation()"></cs-page-btn>
<cs-field field="$ctrl.CvReportModel.Product" ng-model="$ctrl.UploadedFile.Product"></cs-field>
<cs-field field="$ctrl.CvReportModel.Month" ng-model="$ctrl.UploadedFile.Month"></cs-field>
So I am trying to get output like:
CvReportModel.IsReady
confirmation()
CvReportModel.Product
CvReportModel.Month
I am trying to do it using Get-Content and Select-String but still not able to get the desired output.

Use the Get-Content cmdlet to read your file and use a regex to fetch your desired content:
$content = Get-Content 'your_file_path' -raw
$matches = [regex]::Matches($content, '"\$ctrl\.([^"]+)')
$matches | ForEach-Object {
$_.Groups[1].Value
}
Regex:
"\$ctrl\.[^"]+
Output:
CvReportModel.IsReady
confirmation()
CvReportModel.Product
UploadedFile.Product
CvReportModel.Month
UploadedFile.Month
Another approach using the Select-String cmdlet and a regex with positive lookbehind:
Select-String -Path $scripts.tmp -Pattern '(?<=\$ctrl\.)[^"]+' |
ForEach-Object { $_.Matches.Value }
Output:
CvReportModel.IsReady
confirmation()
CvReportModel.Product
CvReportModel.Month
Note:
This will only return the first $ctrl.* match of each line. But since this matches your desired output it could be usefull for you.

Related

Get a string using Select-String method in Powershell

I have many text files like these and below is a sample from one such file. This is part of a file File_Content12.
name:duplicate1-content:philips -sequence:primary...........
name:guard1-content:sony -sequence:primary...........
name:Linux-content:sony -sequence:third...........
name:Windows-content:IPS -sequence:secondary...........
name:Notebook-content:Mark -sequence:fourth...........
name:duplicate1-content:Tony -sequence:primary...........
I'm writing a powershell code to get the name duplicate1 when the content is sony.
I've written the below code and it gets the information as which content is present in which file, line number along with the line.
$contents = 'sony','Philips'
ForEach ($ct in $contents)
{
Get-Childitem -Path "D:\Input\Files\" | Select-String -Pattern "$ct" | Select Filename,LineNumber,Line,#{n='Content';e={$ct}}
}
I'm stuck on this part to get the name duplicate1 when the content is sony. Do I need to use the IndexOf and Substring to get the value or is there any other way to get this name.
Select-String uses Regular Expression so duplicate.*sony should do what you need
$contents = 'duplicate.*sony','Philips'
ForEach ($ct in $contents)
{
Get-Childitem -Path "D:\Input\Files\" | Select-String -Pattern "$ct" | Select Filename,LineNumber,Line,#{n='Content';e={$ct}}
}
Run Get-Help about_Regular_Expressions for more details

Splitting in Powershell

I want to be able to split some text out of a txtfile:
For example:
Brackets#Release 1.11.6#Path-to-Brackets
Atom#v1.4#Path-to-Atom
I just want to have the "Release 1.11.6" part. I am doing a where-object starts with Brackets but I don't know the full syntax. Here is my code:
"Get-Content -Path thisfile.txt | Where-Object{$_ < IM STUCK HERE > !
You could do this:
((Get-Content thisfile.txt | Where-Object { $_ -match '^Brackets' }) -Split '#')[1]
This uses the -match operator to filter out any lines that don't start with Brackets (the ^ special regex character indicates that what follows must be at the beginning of the line). Then it uses the -Split operator to split those lines on # and then it uses the array index [1] to get the second element of the split (arrays start at 0).
Note that this will throw an error if the split on # doesn't return at least two elements and it assumes that the text you want is always the second of those elements.
$bracketsRelease = Get-Content -path thisfile.txt | foreach-object {
if ( $_ -match 'Brackets#(Release [^#]+)#' )
{
$Matches[1]
}
}
or
(select-string -Path file.txt -Pattern 'Brackets#(Release [^#]+)#').Matches[0].Groups[1].value

Powershell append string to text creates a new line

Im trying to create a ps script which appends the string ;history directly after ;stylesheets but currently the script is creating an additional line when i want it to append it directly after.
This is how it is suppose to look
;stylesheets;history
But this is what happens
;stylesheets
;history
Can someone explain why?
(Get-Content K:\Temp\test.properties) |
Foreach-Object {
$_
if ($_ -match ";stylesheets")
{ ";history"
}
} | Set-Content K:\Temp\test.properties
Alternative solution:
(Get-Content K:\Temp\test.properties).Replace(';stylesheets',';stylesheets;history') |
Set-Content K:\Temp\test.properties
You are getting two lines because you are returning two objects when you match. #($_,";history") and each object is written as a line. You could combine them into one string when you match.
(Get-Content K:\Temp\test.properties) |
Foreach-Object {
if ($_ -match ";stylesheets") {
"$_;history"
} else {
$_
}
} | Set-Content K:\Temp\test.properties

powershell Parsing for multiple keywords and sending output to a text file

I'm trying to write a powershell cmdlet to find multiple words in lines in file. Example. I need to parse "word1", "word2", "word3" are in the same line of a file. I'm doing something wrong because I tried this with no success:
(gci -File -Filter FileName | Select-String -SimpleMatch word1, word2,word3) > outputFileName.txt
where FileName = name of file, outputFileName = generated file from my search of the three words. Thank you.
Select-String doesn't have any combination operator that I can think of. If your words were always in that order, then you could do -Pattern 'word1.*word2.*word3' as your match, but if they could be in any order that would get complex very quickly. Instead, I'd look at
.. | Select-String 'word1' | Select-String 'word2' | Select-String 'Word3'
so, all the lines which match word1. Out of those, the ones which match word2 somewhere. Out of that even smaller result, the ones which also match word3.
try this:
$wordlist=#("word1", "word2", "word3")
Get-ChildItem "c:\temp\" -file | %{$currentfile=$_.FullName; Get-Content $_.FullName |
%{
$founded=$true
foreach ($item in $wordlist)
{
if (!$_.Contains($item))
{
$founded=$false
break
}
}
if ($founded)
{
$currentfile
}
}
}

giving string without first two or three letter in power shell

i want to read a string from a line. The line is
ERROR: file'C:\Program Files (x86)\movies\action\Theincrediblehulk.mp3' is missing
I want only
Theincrediblehulk.mp3
the code i use to get only that string is
Select-String tt.txt -pattern [regex]"[A-Za-z]+\.mp3" -AllMatches | % { $_.Matches } | % { $_.Value }
but it is giving me the output as
crediblehulk.mp3
first two or three words is missing.
Please suggest a better way to solve this. i use [A-Za-z] because name is dynamic.
Start by grabbing everything in between the quotes:
$Filepaths = Select-String tt.txt -pattern "'([^']+)'" -AllMatches | % { $_.Matches } | % { $_.Groups[1].Value }
Now you can use either Split-Path -Leaf to grab the file name:
$Filenames = $Filepaths |Split-Path -Leaf
or Path.GetFileName():
$Filenames = $Filepaths |% { [System.IO.Path]::GetFileName($_) }
Mathias R. Jessen's answer is a better solution, however this answer explains why the original code isn't working as expected.
I assume by your [regex] at the beginning, you're trying to tell powershell to convert your string to a regex object. However powershell is actually interpreting your argument as
-pattern '[regex]"[A-Za-z]+\.mp3"'
If you do want to explicitly treat your string as a regex object, you'll need to wrap the value in parentheses
-pattern ([regex]"[A-Za-z]+\.mp3")
Although the regex cast is not necessary, so the string alone is sufficient.
-pattern '[A-Za-z]+\.mp3'