How to search for a string that starts with $ in a txt file?
For example:
Get-Content $file | Select-String -pattern 'VALUE="$USERPROFILE"'
Thanks
Either use the [regex]::Escape() method to correctly escape verbatim strings for use in regex patterns:
$pattern = 'VALUE="{0}"' -f [regex]::Escape('$USERPROFILE')
Get-Content $file | Select-String -Pattern $pattern
or use the -SimpleMatch switch to indicate you don't wanna use regex at all:
Get-Content $file | Select-String -pattern 'VALUE="$USERPROFILE"' -SimpleMatch
If you only want to find strings that start with '$', you can use an approach similar to the one below.
param(
[string] $file = "$PSScriptRoot\test.txt",
[string] $pattern = "$*"
)
$stringsBeginningWithPattern = (Get-Content $file).Split() | Where-Object {
$_ -like $pattern
}
$stringsBeginningWithPattern
Related
I have several single line xml files out of which I want to extract the contents of a particular tag <LIFNR>. I managed doing that using the following two lines of power shell:
get-content P:\Webservice\21*_*CR_d*.xml | foreach-object { $_ -replace '>', ">`r`n" } > xmls_newline.txt
get-content .\xmls_newline.txt | Select-String -Pattern '.*<\/LIFNR>'
However if I want to skip the step of creating an intermediary text file I cannot achieve the same result.
$xml = get-content P:\Webservice\21*_*CR_d*.xml | foreach-object { $_ -replace '>', ">`r`n" }
$xml | Select-String -Pattern '.*<\/LIFNR>'
or
get-content P:\Webservice\21*_*CR_d*.xml | foreach-object { $_ -replace '>', ">`r`n" } | Select-String -Pattern '.*<\/LIFNR>'
each just print the result of the string splitting in the foreach-object statement, the Select-String command is ignored.
Can somebody explain to me what is different about the latter two attempts compared to the working solution?
Get-Content outputs each line as a separate string. To emulate the same behavior, split the string after > instead of appending a linebreak:
Get-Content P:\Webservice\21*_*CR_d*.xml |ForEach-Object { $_ -split '(?<=>)' } | Select-String -Pattern '.*<\/LIFNR>'
The construct (?<=...) is a lookbehind assertion - this way, PowerShell will split on a zero-length string immediately after >
I need to check the contents line by line by selecting the string of the line.
I have declared each line matching string to a variable.Is it possible to use switch cases for looking into the variables
I have used if condition to check the each line with the help of variables .
I wanted to know whether we could use switch cases?
Consider file.txt having following contents :
$q = Get-Content -Path .\file.txt |Select-String 'Hello' -SimpleMatch
$w = Get-Content -Path .\file.txt |Select-String 'new' -SimpleMatch
$e = Get-Content -Path .\file.txt |Select-String 'World' -SimpleMatch
$r = Get-Content -Path .\file.txt |Select-String 'Hi' -SimpleMatch
$t = Get-Content -Path .\file.txt |Select-String 'greet' -SimpleMatch
So can we check the variables using the switch options. Is it possible to output the variables which are not present. How can we achieve the same if the file contents are large?
Can we use switch options and output the desired result?
If you just want to know which of the 5 words are not in any of the lines in the file, you can do this
$Pattern = 'Hello|new|World|Hi|greet'
$Test = (Get-Content -Path .\file.txt | Select-String -Pattern $Pattern -AllMatches).foreach{$_.matches.Value}
$($pattern -split '\|').where{$Test -notcontains $_}
But is that your goal?
I have a text (.txt) file with following content:
Car1
Car2
Car3
Car4
Car5
For changing Car1 for random text I used this script:
Get-ChildItem "C:\Users\boris.magdic\Desktop\q" -Filter *.TXT |
Foreach-Object{
$content = Get-Content $_.FullName
$content | ForEach-Object { $_ -replace "Car1", "random_text" } | Set-Content $_.FullName
}
This is working ok, but now I want to add one text line under Car2 in my text file.
How can I do that?
Just chain another -replace and use a new line!
Get-ChildItem "C:\Users\boris.magdic\Desktop\q" -Filter *.TXT |
Foreach-Object{
$file = $_.FullName
$content = Get-Content $file
$content | ForEach-Object { $_ -replace "Car1", "random_text" -replace "(Car2)","`$1`r`nOtherText" } | Set-Content $file
}
First thing is that | Set-Content $_.FullName would not work since the file object does not exist in that pipe. So one simple this to do it save the variable for use later in the pipe. You can also use the ForEach($file in (Get-ChildItem....)) construct.
The specific change to get what you want is the second -replace. We place what you want to match in brackets to that we can reference it in the replacement string with $1. We use a backtick to ensure PowerShell does not treat it as a variable.
We can remove some redundancy as well since -replace will work against the strings of file as a whole
Get-ChildItem "c:\temp" -Filter *.TXT |
Foreach-Object{
$file = $_.FullName
(Get-Content $file) -replace "Car1", "random_text" -replace "(Car2)","`$1`r`nOtherText" | Set-Content $file
}
While this does work with your sample text I want to point out that more complicated strings might require more finesse to ensure you make the correct changed and that the replacements we are using are regex based and do not need to be for this specific example.
.Replace()
So if you were just doing simple replacements then we can update your original logic.
Foreach-Object{
$file = $_.FullName
$content = Get-Content $_.FullName
$content | ForEach-Object { $_.replace("Car1", "random_text").replace("Car2","Car2`r`nOtherText")} | Set-Content $file
}
So that is just simple text replacement chained using the string method .Replace()
I am trying to replace in several files the following type of string:
StringResourceHelper.[STRING_OF_INTEREST]ResourceString()
with
Strings.[STRING_OF_INTEREST]
For example I would like to replace:
StringResourceHelper.HelpResourceString() with Strings.Help
So far, I got a simple replacement which would work if the second string would not be important:
Get-ChildItem . *.cs -Recurse |
Foreach-Object {
$c = ($_ | Get-Content)
$c = $c -replace 'StringResourceHelper.','Strings.'
$c | Set-Content $_.FullName -Encoding UTF8
}
But this does not help me, because I also have Strings like StringResourceHelper.Culture which should not be touched.
Any help is appreciated.
You need to escape literal dots in your regular expression, otherwise . will match any character except newlines. Something like this should work:
$c = $c -replace 'StringResourceHelper\.(\w*?)ResourceString\(\)', 'Strings.$1'
Also, your code could be simplified a little, like this:
$srch = 'StringResourceHelper\.(\w*?)ResourceString\(\)'
$repl = 'Strings.$1'
Get-ChildItem . *.cs -Recurse | % {
$filename = $_.FullName
(Get-Content $filename) -replace $srch, $repl |
Set-Content $filename -Encoding UTF8
}
I am trying to find all files, which does not contains a selected string. Find files which contains is easy:
gci | select-string "something"
but I do not have an idea how to negate this statement.
You can use Where-Object;
gci | Where-Object { !( $_ | Select-String "something" -quiet) }
I'm not sure if it can be done without the foreach-object but this works:
gci |foreach-object{if (-not (select-string -inputobject $_ -Pattern "something")){$_}}
As mentionend in How do I output lines that do not match 'this_string' using Get-Content and Select-String in PowerShell?
Select-String has the NotMatch parameter.
So you could use it:
gci | Select-String -notmatch "something"
foreach($line in Get-Content .\file.txt)
{
if(findstr $line "dummyText.txt" ){
# Work here or skip here
}
else {
echo $line
}