Powershell to truncate a string - string

I'm trying to truncate a string which I'm fetching from an excel file.
The string(URL) is: ;#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#
I need it to be: abc.com/sites/abcx/000000103483/
But the problem is abcx in the URL ranges from abcx to abcxxxx, where x is an integer.
How can I get the required result?

In PowerShell, you could split on /s and build the string you want like this:
$string = ";#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#"
$split = $string.Split("/")
$split[2] + "/" + $split[3] + "/" + $split[4] + "/" + $split[5] + "/"
Output:
abc.com/sites/abcx/000000103483/

Remove default.aspx and everything that comes behind it.
$s = ';#://abc.com/sites/abcx/000000103483/default.aspx;#000000103483;#'
$s -replace 'default\.aspx.*'

Related

Add character between column combine in a CSV

I'm looking for the correct syntax to add some (") between my variable.
I need something like that :
"firstname","lastname","email","",""
Here is the first script I have :
foreach($line in Get-Content .\extract.csv)
{ $firstname = $line.split(';')[0]
$lastname = $line.split(';')[1]
$email = $line.split(';')[2]
$newLine = "$firstname - $lastname - $email"
echo $newLine }
I'm really new in scripting and I'm a bit lost with all these (') (")
My second question is : I need to extract my data only from the second row and ignore the first one, can you help me for this too ?
Thanks !
Have you try escaping your " and ' ?
In powershell you can use backtick ` (AltGr + 7) or doubling the char to do so :
Example :
Write-Host(" `" ")
Write-Host(" "" ")
Please add more code if this doesn't solve you issue !

Powershell: difference between assignment and parameter for cmdlet (parsing)

I am new to powershell language and I have problems understanding some basic concepts regarding string concatenation.
I tried to concat a string with the + char as I knew it from other programming languages i. e. Java.
line 1: $result = 7
line 2: Write-Host "Result: " + $result + "!" # Result: + 7 + !
I then realized (i. e. in this question How do I concatenate strings and variables in PowerShell?) that I need to do it (in one of) the powershell way(s); for example like this.
line 3: Write-Host "Result: $result!" # Result: 7!
As I experimented a little I found out that if I assign the expression in line 2 to a variable it somehow works as I anticipated it in the first place.
line 4: $str = "Result: " + $result + "!"
line 5: Write-Host $str # Result: 7!
So my question is, why is there a difference if I pass a Java-style concatenated string to Write-Output cmdlet or if I assign the same string to a variable?
String concatenation and expansion is a bit different in PowerShell, here are several ways to accomplish it-
Format operator:
PS C:\> 'This exhibits {0} string expansions! {1} {0}!' -f #(2,'Wow!')
This exhibits 2 string expansions! Wow! 2!
Each item in the array is accessed by the number in the braces.
Subexpression:
PS C:\> "Sometimes you need calculations in a string. 5 + 3 = $(5 + 3)"
Sometimes you need calculations in a string. 5 + 3 = 8
An explanation here: this will not work with string literals, i.e., ''.
Everything contained in the subexpression will be evaluated and converted to a string if possible utilizing an object's ToString() method.
String Expansion:
PS C:\> $Var = 'This string'
PS C:\> "$Var is amazing!"
This string is amazing!
This will also not work with string literals, i.e., ''. If you need to concatenate a variable-qualifying character next to the variable call, you can use curly braces to avoid a null value, i.e., ${Var}_notattached
String Concatenation:
Tried and true:
PS C:\> 'Sometimes you just ' + 'need to add. 8 = ' + 8
Sometimes you just need to add. 8 = 8

How to remove the left side of this string?

My string is:
$dst = "Folder_1\SubFolder_2\3\4\5"
My goal is to have:
$dst_OK = "SubFolder_2\3\4\5"
I tried use split function like this:
$dst_OK = $dst.split("\")[0]
but the result is Folder_1 only.
You could use the following regex to remove the left side of the string:
$dst_OK = $dst -replace '^.*?\\'
However, since it looks like you are dealing with a path, you may consider to using builtin function within the System.IO.Path namespace.
You can do it with this snippet:
$first, $rest = "Folder_1\SubFolder_2\3\4\5" -split '\\'
$rest = $rest -join '\'
Other solution :
($dst -split "\\", 2)[1]
Solution 2
$dst.Substring($dst.IndexOf('\')+1)

how to remove multiple whitespaces and newline character from HTML entity

I am trying to implement a crawler using codeigniter and simplehtmldom.
$page = "URL to be Crawled";
$html = file_get_html($page);
$ad_description = $html->find('#ad_description',-1);
$description = $ad_description->innertext;
$description contains multiple consecutive spaces and newline which I need to convert in to single appearances.
I tried
str_replace("\n\r",' ',$description),
reduce_multiples($ad_description->innertext,"\r")
preg_replace("/[\r\n]+/", "\n", $description)
ascii_to_entities($description,ENT_HTML5, "ISO-8859-1")
and many other possible options but without success. Any help would be appreciated.
i think that pref_replace does work
$description = "This
is a
test string
";
echo $description = preg_replace('/\s+/', ' ', $description); // This is a test string

remove specific character from a string with batch script

i want to remove only a specific character into a string and only one time. for example, if I have this file:
"1234 + test.txt"
i want to remove "+" character. my problem is that I don't know how many "+" could be in filename; by the way, I want to remove only the first:
"1234 ++ test + hello + world.txt"
need to be:
"1234 + test + hello + world.txt"
I need to do this with a bat script. I have some problems to use correctly "token,delims" parameters....
edit: I've a problem with Edoro's solution. if filename is "++plus--.txt", %left% is "plus--.txt" and %right% is +plus--.txt
pure batch
#echo off &setlocal
set "string=1234 ++ test + hello + world.txt"
for /f "delims=+" %%i in ("%string%") do set "left=%%i"
set "right=%string:*+=%"
set "new=%left%%right%"
echo %new%
..output is:
1234 + test + hello + world.txt
Go through a sed script:
echo "1234 + test + hello + world.txt" | sed 's/+//'

Resources