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 !
Related
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
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)
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.*'
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
I'm using Windows 7. I have a bunch of text files, each containing one email message. Each starts this way:
FROM: Person
TO: Another Person
DATE: 01-Jan-11 at 18:12:00
SUBJECT: Whatever
I want to rename these files so that their names look like this:
2011-01-01 18.12 Email from Person to Another Person re Whatever.txt
Batch programming is all I know, and I don't know it very well. For purposes of restraining this to a project that I can understand quickly, I think my best solution will be to extract the essential data into a text file that I can then massage into a batch renaming file.
In that case, what I'm looking for is a batch file that will extract the data into single lines in a text file that I can then massage into shape with global edits. In other words, I think I'm looking for text lines in this format:
[current filename] [extracted date and time string] [from] [to] [subject]
Example:
file01.txt 01-Jan-11 at 18:12:00 from Person to Another Person re Whatever
If I've got lines like that, I can parse them into renaming commands pretty quickly in Excel.
Thanks!
Given that your using Windows 7, I thought I'd suggest an alternative. Windows Powershell is a a very useful command tool that can be used for a ton of stuff. I think I solved your complete problem:
$folder = "C:\..."
$regex = "FROM: (.*) TO: (.*) DATE: (.*) at (.*) SUBJECT: (.*)"
$files = Get-ChildItem $folder *.txt
ForEach ($file in $files) {
$line = (Get-Content $file.FullName -TotalCount 1)
$match = ([regex]$regex).matches($line)[0]
$date = [DateTime]($match.Groups[3]).Value + [TimeSpan]($match.Groups[4]).Value
$from = ($match.Groups[1])
$to = ($match.Groups[2])
$subject = ($match.Groups[5])
# You can change the naming format in the brackets below
Rename-Item $file.FullName -NewName ( $date.ToString("yyyy-MM-dd_HH-mm-ss") + " Email From " + $from + " to " + $to + " RE " + $subject)
}
It makes a few assumptions (like a match will always be found). You can easily adjust naming format and other things. Save this code as a script (.ps1) and run it in the Powershell prompt (powershell.exe)