--deleted earlier text - I asked the wrong question!
ahem....
What I have is $var = "\\unknowntext1\alwaysSame\unknowntext2"
I need to keep only "\\unknowntext1"
Try regular expressions:
$foo = 'something_of_unknown' -replace 'something.*','something'
Or if you know only partially the 'something', then e.g.
'something_of_unknown' -replace '(some[^_]*).*','$1'
'some_of_unknown' -replace '(some[^_]*).*','$1'
'somewhatever_of_unknown' -replace '(some[^_]*).*','$1'
The $1 is reference to group in parenthesis (the (some[^_]*) part).
Edit (after changed question):
If you use regex, then special characters need to be escaped:
"\\unknowntext1\alwaysSame\unknowntext2" -replace '\\\\unknowntext1.*', '\\unknowntext1'
or (another regex magic) use lookbehind like this:
"\\unknowntext1\alwaysSame\unknowntext2" -replace '(?<=\\\\unknowntext1).*', ''
(which is: take anything (.*), but there must be \\unknowntext1 before it ('(?<=\\\\unknowntext1)) and replace it with empty string.
Edit (last)
If you know that there is something known in the middle (the alwaysSame), this might help:
"\\unknowntext1\alwaysSame\unknowntext2" -replace '(.*?)\\alwaysSame.*', '$1'
function Remove-TextAfter {
param (
[Parameter(Mandatory=$true)]
$string,
[Parameter(Mandatory=$true)]
$value,
[Switch]$Insensitive
)
$comparison = [System.StringComparison]"Ordinal"
if($Insensitive) {
$comparison = [System.StringComparison]"OrdinalIgnoreCase"
}
$position = $string.IndexOf($value, $comparison)
if($position -ge 0) {
$string.Substring(0, $position + $value.Length)
}
}
Remove-TextAfter "something_of_unknown" "SoMeThInG" -Insensitive
What I have is $var = "\unknowntext1\alwaysSame\unknowntext2"
I need to keep only "\unknowntext1"
Not sure this requires a regular expression. Assuming alwaysSame is literally always the same, as the discussion around stej's answer suggests, it seems by far the most straightforward way to accomplish this would be:
$var.substring(0, $var.indexOf("\alwaysSame"));
Related
Been scratching my head on this one...
I'd like to remove .com and capitalize S and T from: "sometext.com"
So output would be Some Text
Thank you in advance
For most of this you can use the replace() member of the String object.
The syntax is:
$string = $string.replace('what you want replaced', 'what you will replace it with')
Replace can be used to erase things by using blank quotes '' for the second argument. That's how you can get rid of .com
$string = $string.replace('.com','')
It can also be used to insert things. You can insert a space between some and text like this:
$string = $string.replace('et', 'e t')
Note that using replace does NOT change the original variable. The command below will print "that" to your screen, but the value of $string will still be "this"
$string = 'this'
$string.replace('this', 'that')
You have to set the variable to the new value with =
$string = "this"
$string = $string.replace("this", "that")
This command will change the value of $string to that.
The tricky part here comes in changing the first t to capital T without changing the last t. With strings, replace() replaces every instance of the text.
$string = "text"
$string = $string.replace('t', 'T')
This will set $string to TexT. To get around this, you can use Regex. Regex is a complex topic. Here just know that Regex objects look like strings, but their replace method works a little differently. You can add a number as a third argument to specify how many items to replace
$string = "aaaaaa"
[Regex]$reggie = 'a'
$string = $reggie.replace($string,'a',3)
This code sets $string to AAAaaa.
So here's the final code to change sometext.com to Some Text.
$string = 'sometext.com'
#Use replace() to remove text.
$string = $string.Replace('.com','')
#Use replace() to change text
$string = $string.Replace('s','S')
#Use replace() to insert text.
$string = $string.Replace('et', 'e t')
#Use a Regex object to replace the first instance of a string.
[regex]$pattern = 't'
$string = $pattern.Replace($string, 'T', 1)
What you're trying to achieve isn't well-defined, but here's a concise PowerShell Core solution:
PsCore> 'sometext.com' -replace '\.com$' -replace '^s|t(?!$)', { $_.Value.ToUpper() }
SomeText
-replace '\.com$' removes a literal trailing .com from your input string.
-replace '^s|t(?!$), { ... } matches an s char. at the start (^), and a t that is not (!) at the end ($); (?!...) is a so-called negative look-ahead assertion that looks ahead in the input string without including what it finds in the overall match.
Script block { $_.Value.ToUpper() } is called for each match, and converts the match to uppercase.
-replace (a.k.a -ireplace) is case-INsensitive by default; use -creplace for case-SENSITIVE replacements.
For more information about PowerShell's -replace operator see this answer.
Passing a script block ({ ... }) to dynamically determine the replacement string isn't supported in Windows PowerShell, so a Windows PowerShell solution requires direct use of the .NET [regex] class:
WinPs> [regex]::Replace('sometext.com' -replace '\.com$', '^s|t(?!$)', { param($m) $m.Value.ToUpper() })
SomeText
I have a list of users in a text file who's names are in the following format: xn-tsai-01.
How do I script to remove the xn- KEEP THIS -01 so the output is like: tsai
I know how to do this in bash but not too familiar with powershell.
Thanks in advance!
Why not use Substring method. If you will always trim the first three characters, you can do the following assuming the variable is a string type.
$string = xn-tsai-01
$string.Substring(3)
Here is a quick way to do it using regex:
'xn-tsai-01' -replace '.*?-(.*)-.*','$1'
Example with a list:
(Get-Content list.txt) -Replace '.*?-(.*)-.*','$1'
You can use the .NET string method IndexOf("-") to find the first, and LastIndexOf("-") to find the last occurrence of "-" within the string.
Use these indexes with Substring() to remove the unnecessary parts:
function Clean-Username {
param($Name)
$FirstDash = $Name.IndexOf("-") + 1
$LastDash = $Name.LastIndexOf("-")
return $Name.Substring( $f, $l - $f )
}
PS C:\> Clean-UserName -Name "xn-tsai-01"
tsai
Boe's example is probably going to be the most efficient.
Another way is to use the split() method if they're in a uniform format.
Get-Content .\list.txt | % { ($_.Split('-'))[1] }
% is an alias for ForEach
I have a method that has an if statement that catches if it finds a special character. What I want to do now if find the position of the special characters and replace it with _A
Some Examples
test# becomes test_A
I#hope#someone#knows#the#answer# becomes I_Ahope_Asomeone_Aknows_Athe_Aanswer_A
or if it has more than one special character
You?didnt#understand{my?Question# becomes You_Adidnt_Aunderstand_Amy_AQuestion_A
Would I have to loop through the whole string and when I reach that character change it to _A or is there a quicker way of doing this?
# is just a character like any other, you can use the -replace operator:
PS C:\>'I#hope#someone#knows#the#answer#' -replace '#','_A'
I_Ahope_Asomeone_Aknows_Athe_Aanswer_A
Regex is magic, you can define all the special cases you like (braces will have to be escaped):
PS C:\>'You?didnt#understand{my?Question#' -replace '[#?\{]','_A'
You_Adidnt_Aunderstand_Amy_AQuestion_A
So your function could look something like this:
function Replace-SpecialChars {
param($InputString)
$SpecialChars = '[#?\{\[\(\)\]\}]'
$Replacement = '_A'
$InputString -replace $SpecialChars,$Replacement
}
Replace-SpecialChars -InputString 'You?didnt#write{a]very[good?Question#'
If you are unsure of which characters to escape, have the regex class do it for you!
function Replace-SpecialChars {
param(
[string]$InputString,
[string]$Replacement = "_A",
[string]$SpecialChars = "#?()[]{}"
)
$rePattern = ($SpecialChars.ToCharArray() |ForEach-Object { [regex]::Escape($_) }) -join "|"
$InputString -replace $rePattern,$Replacement
}
Alternatively, you can use the .NET string method Replace():
'You?didnt#understand{my?Question#'.Replace('#','_A').Replace('?','_A').Replace('{','_A')
But I feel the regex way is more concise
In Powershell given the following string
$string = "this is a sample of 'my' text $PSP.what do you think"
how do I use the -replace function to convert the string to
this is a sample of 'my' text Hello.what do you think
I obviously need to escape the string somehow, Also $PSP is not a declared variable in my script
I need to change all mentions of $PSP for some other string
Use the backtick character (above the tab key):
$string = "this is a sample of 'my' text `$PSP.what do you think"
To replace the dollar sign using the -replace operator, escape it with backslash:
"this is a sample of 'my' text `$PSP.what do you think" -replace '\$PSP', 'hello'
Or use the string.replace method:
$string = "this is a sample of 'my' text `$PSP.what do you think"
$string.Replace('$PSP','Hello)'
this is a sample of 'my' text Hello.what do you think
Unless you modify your original string (e.g. by escaping the $), this is isn't (really) possible.
Your $string doesn't really contain a $PSP, as it is replaced by nothing in the assignment statement.
$string = "this is a sample of 'my' text $PSP.what do you think"
$string -eq "this is a sample of 'my' text .what do you think"
evaluates to:
True
This comes up as the first answer in google even though it is really old, so I will add my slight variation.
In my case I was reading in a file and replacing a string with $s in it.
The short version of my file is:
<version>$version$<version>
In the case where one is actiong on a (file) stream, variables are not autoreplaced so there is no need to escape the $ in the file.
In the replacement pattern you can avoid the interpretation of the variable using ' instead of ".
My final command looked like:
(gc $fileName) | % { $_.replace('$version$', "$BuildNumber") } | sc $fileName
This is a file read(get-content) piped through the replace and back in to the file with a set-content.
You should try
$string = $string.Replace("\$PSP", "Hello")
or
$string = $string.Replace("\$PSP", $the_new_value)
or to be more generic use Regex
$string = [regex]::Replace($string, "\$\w+", "Hello")
There is probably an easy solution to this, but I can't figure it out. I am looking to:
take a CSV file into an array
loop through the array and split fields into variables
if the array field is empty then set the variable to "N/A"
Note: It is only setting the $variable to "N/A" that I cannot get working.
For example:
foreach $var (#list) {
($name,$date,$size, etc...)=split(/,\"/,$var);
}
How would I set $date to "N/A" if the field in the array is empty?
so to produce:
$name = Jim
$date = N/A
$size = small
I hope this makes sense and is easy to fix.
-Thanks
Assuming the variable $date is undefined when "empty":
if (!defined($date)) {
$date = 'N/A';
}
Or more concisely:
$date //= 'N/A';
Or if it really is an empty string, i.e. $date = ''; (this will also work in the case where $date is undefined, but you don't want to use this if you only want to identify the case where it is undefined):
if ($date eq '') {
$date = 'N/A';
}
Or more concisely (note that this will also set $date to N/A if $date is '0' due to Perl's weak typing):
$date ||= 'N/A';
As far as your third bullet point and the actual question: to check for emptiness:
For empty string, you can either do the above-mentioned eq "", or you can check the string length: $var = "N/A" unless length($var);;
For an undefined of empty string, in Perl 5.10 you can use the "defined-or" (//) operator to do the short version: $var = "N/A" unless length($var // '');
In Perl before 5.10 where "defined-or" is not available, you will either have to spell out the defined check: $var = "N/A" unless defined $var && length($var);
... or, you can just stop caring about undefined warnings by turning them off (h/t brian d foy):
no warnings 'uninitialized';
$_ = "N/A" unless length($_) foreach ($name,$date,$size, etc...);
use warnings 'uninitialized'; # Always turn back on.
However, please note that you also should consider a different approach to the first two bullet points. Implementing your own CSV parser which is 100% correct is not trivial - for example, your sample code will break if any of the fields contain a double quote.
Instead, you should always use one of the standard Perl CSV parsers, such as Text::CSV_XS.
$name = "N/A" if (!defined($name) || ($name eq ""))
$date = "N/A" if (!defined($date) || ($date eq ""))
$size = "N/A" if (!defined($size) || ($size eq ""))
Make sure you are using string comparison for comparing strings :)
What will be the input like if date is missing? If the input is: somename,200 (where 200 is size), then date would be set as 200 right?
If the input is like this
somename,,200
where 200 is size, and because date is unavailable it is set to empty. Then you can do a simple if-check:
if($date eq '')
{
$date = "NA";
}
Note $date will be defined, it will be just set to empty
if ($date eq '') { print "STRING IS EMPTY\n" } else { Print "STRING IS NOT EMPTY\n";}
we can use the above code to identify the empty string ,and using the regular expression is more efficient. The "=~" operator and using regular expression also we can also this problem.