PowerShell Çok Satırlı Dize | Onun dizesini kullanarak çok satırlı dizenin çalışması


PowerShell Çok Satırlı Dizeye Giriş

Bir dize, “.txt” içine alınmış bir karakter kümesinden başka bir şey değildir. Çok satırlı dize, değeri bir satırın ötesine geçen bir dizedir. Bu durumda, dize bir burada-string(@ ” ”@) içine alınmalıdır. Çok satırlı bir dize oluşturmak için yeni satır veya satırbaşı değeri de kullanılabilir. Çok satırlı dize, çift tırnak içinde satır sonu kullanılarak da tanımlanabilir. Bu makale, çok satırlı dize ve sözdiziminin kullanımlarını uygun örneklerle birlikte ayrıntılı olarak açıklayacaktır.

Sözdizimi:

Here-string, çok satırlı dizeyi aşağıdaki gibi tanımlamak için kullanılır. İlk ve son satır hiçbir şey içermemelidir.

@”
First-line
Second line
Third line
“@

Değerlendirilmeyecek değişken veya ifadeyi yapmadığımızda dize değeri tek tırnak içine alınabilir.

Örnek:

Write-Host "example of multiline string using here string"
$test=@"
First-line
second line
"@
Write-Host $test
Write-Host "example using literal string"
$test1=@'
first line
$(Get-Date)
second line
'@
Write-Host $test1

Çıktı:

Burada-dizgesi olmadan bile çok satırlı dize aşağıdaki gibi tanımlanabilir

Giriş:

Write-Host "example of multiline without here string"
$text = {
Myself vignesh
am from chennai
am a freelancer
this is fourth line
}
write-Host $text.ToString()

Çıktı:

Çok Satırlı String'in String'ini kullanarak çalışması

PowerShell, bir @ ” ve ardından yeni satırın geldiğini gördüğünde bir dizenin çok satırlı olduğunu anlar. “@” ile bittiği zaman biter. Bunun temel amacı kullanıcıların tırnak işaretleri, semboller, yeni satırlar veya diğer biçimlendirme metinleri içeren dizeler oluşturmasına olanak sağlamasıdır. Çok satırlı dizgeye referansla bir hata oluştuğunda, ilk önce ilk satırı kontrol etmek akıllıca olacaktır; @ ile “ arasında boşluk yoktur. Normal dize değişkeni gibi çok satırlı dize de değişmez veya genişleyen olabilir. Tek tırnaklar gerçek değeri, çift tırnaklar ise genişlemeyi temsil eder. Bir değer veya ifade, tek tırnaklı bir çoklu dize içinde kullanıldığında, değişmez bir dize olarak yazdırılır; oysa bir değişken veya ifade, çift tırnaklı bir çoklu dize içinde kullanılırsa, değerlendirilir ve bunun yerine gerçek değer yazdırılır. gerçek değerinden daha fazla. Kullanıcının ihtiyaçlarına göre çok satırlı dizilerin dört olası kombinasyonu oluşturulabilir. Aşağıdaki örneklerde çok hatlı dizinin çeşitli senaryoları ayrıntılı olarak tartışılmaktadır.

Örnekler

PowerShell Multiline String örneklerini tartışalım.

Örnek #1

Write-Host "Converting multine line string to a single line" -ForegroundColor Green
Write-Host "Get the contents of the file before converting" -ForegroundColor Green
Write-Host "Displaying the contents of the file" -ForegroundColor Green
$input = Get-Content -Path "C:\vignesh\multiline.txt"
foreach($line in $input)
{
Write-Host $line
}
Write-Host "Merging the contents of the file into a single line" -ForegroundColor Green
foreach ($line in $input)
{
$op = $op + $line
}
$op| Out-File C:\vignesh\multiline.txt -Force
Write-Host "After merging" -ForegroundColor Green
$final = Get-Content -Path "C:\vignesh\multiline.txt"
Write-Host $final

Çıktı:

Örnek #2

Write-Host "Demo of splitting multiline string into object" -ForegroundColor Green
Write-Host " the multiline string is as follows" -ForegroundColor Green
$ip = @"
Student Name: Vignesh Student Id: 1 Age: 28
Student Name: Nandhini Student Id: 2 Age: 29
Student Name: Vyapini Student Id: 3 Age: 30
"@
Write-Host "Before splitting into object" -ForegroundColor Green
Write-Host $ip
Write-Host "After splitting the multiline string into object" -ForegroundColor Green
$ip -split "`n" |
Select-String 'Student Name: (\w+) Student Id: (\w+) Age: (.+)' |
ForEach-Object {
New-Object PSObject -Property ([Ordered] @{
"Student Name" = $_.Matches[0].Groups[1].Value
"Student Id"= $_.Matches[0].Groups[2].Value
"Age" = $_.Matches[0].Groups[3].Value
})
}

Çıktı:

Örnek #3

Write-Host "Demo of different types of multi-string" -ForegroundColor Green
Write-Host "Simple example of multi-string enclosed in double quotes" -ForegroundColor Green
$ip = "
This is first line.
This is second line.
This is third line
"
Write-Host $ip
Write-Host "Simple example of multi-string in single quotes" -ForegroundColor Green
$ip = '
This is first line.
This is second line.
This is third line
'
Write-Host $ip
Write-Host "Example of here string in double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is second line.
This is third line
"@
Write-Host $ip
Write-Host "Example of here string in single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is second line.
This is third line
'@
Write-Host $ip
Write-Host "using value in double quotes" -ForegroundColor Green
$date=Get-Date
$ip=@"
Todays date is below
$date
"@
Write-Host $ip
Write-Host "using value in single quotes" -ForegroundColor Green
$date=Get-Date
$ip=@'
Todays date is below
$date
'@
Write-Host $ip
Write-Host "in single quotes the string literal is printed and not the value" -ForegroundColor Green
Write-Host "Using double quotes inside a double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is "second" line.
This is third line
"@
Write-Host $ip
Write-Host "Using single quotes inside a double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is 'second' line.
This is third line
"@
Write-Host $ip
Write-Host "Using single quotes inside a single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is 'second' line.
This is third line
'@
Write-Host $ip
Write-Host "Using double quotes inside a single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is "second" line.
This is third line
'@
Write-Host $ip

Çıktı:

Çözüm

Böylece makale PowerShell'deki çok satırlı dizeyi ayrıntılı olarak ele aldı. Ayrıca burada-string, tek tırnak, çift tırnak yöntemleri gibi çeşitli yöntemler kullanılarak çok satırlı dizenin nasıl oluşturulabileceği ve bunların farkları da ayrıntılı olarak anlatıldı. Ayrıca, çok satırlı dizelerin nasıl oluşturulacağı ve onlarla nasıl çalışılacağı konusunda bize yol gösteren çeşitli örnek programları da gösterdi. Daha ayrıntılı bilgi edinmek için örnek komut dosyaları yazmanız ve bunları uygulamanız önerilir.

İlgili makaleler