PowerShell ตรวจสอบว่าไฟล์มีสตริงหรือไม่ [6 วิธี]
ใช้ Select-String Cmdlet
ใช้ Select-String cmdlet เพื่อตรวจสอบว่าไฟล์ที่ระบุมีสตริงที่กำหนดใน PowerShell หรือไม่
ใช้ Select-String Cmdlet:
$path = "E:\Test\file1.txt"
$string = "customers"
$result = Select-String $path -Pattern $string
if ($result -ne $null) {
Write-Host "The specified text file contains the string '$string'."
} else {
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file contains the string 'customers'.ขั้นแรก เราประกาศและเริ่มต้นตัวแปรชื่อ $path ซึ่งมีตำแหน่งของไฟล์ file1.txt จากนั้น เราได้สร้างตัวแปรอีกตัวหนึ่งชื่อ $string และตั้งค่าด้วย "customers"; นี่คือค่าที่เรากำลังมองหาใน file1.txt
หลังจากนั้น เราใช้ Select-String พร้อมด้วยพารามิเตอร์ -Pattern เพื่อค้นหา $string ที่ระบุในไฟล์ที่อยู่ใน $path และบันทึกไว้ ค่าที่ส่งคืนในตัวแปร $result
Select-String cmdlet ใช้เพื่อค้นหาสตริงที่ระบุในสตริงและไฟล์อื่น เราสามารถใช้สตริงที่ตรงกับที่ต้องการหรือกำหนดนิพจน์ทั่วไปให้ตรงกับรูปแบบได้ เราใช้พารามิเตอร์ -Pattern เพื่อกล่าวถึงสตริงหรือรูปแบบการค้นหา
Select-String ขึ้นอยู่กับบรรทัดของข้อความ โดยค่าเริ่มต้น จะค้นหารายการที่ตรงกันในทุกบรรทัดของข้อความ และสำหรับการจับคู่ทุกรายการ จะแสดงชื่อไฟล์ หมายเลขบรรทัด และข้อความทั้งหมดที่มีรายการที่ตรงกัน เนื่องจากเรากังวลว่าจะพบสตริงการค้นหาหรือไม่ เราจึงใช้บล็อก if-else พร้อมด้วยตัวดำเนินการ -ne เพื่อตรวจสอบว่า $result ตัวแปรไม่เท่ากับ $null
ถ้าไม่เช่นนั้น เราใช้ Write-Host cmdlet เพื่อพิมพ์ข้อความแจ้งว่าพบสตริงแล้ว มิฉะนั้นเราจะพิมพ์ข้อความแจ้งว่าไม่พบสตริง
Select-Stringcmdlet คล้ายกับคำสั่งgrepใน UNIX หรือfindstr.exeในระบบปฏิบัติการ Windows
ตามค่าเริ่มต้น ตัวเลือก Select-String พร้อมด้วย -Pattern จะดำเนินการจับคู่โดยคำนึงถึงขนาดตัวพิมพ์ ซึ่งหมายความว่าโค้ดด้านบนจะทำงานได้ดีหากเราแทนที่ $string="ลูกค้า" ด้วย $string="CUSTOMERS" อย่างไรก็ตาม หากคุณมีข้อกำหนดโปรเจ็กต์สำหรับการจับคู่แบบคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ เราสามารถใช้พารามิเตอร์ -CaseSensitive ได้ดังต่อไปนี้
ใช้ Select-String กับ -CaseSensitive:
$path = "E:\Test\file1.txt"
$string = "Customers"
$result = Select-String $path -Pattern $string -CaseSensitive
if ($result -ne $null) {
Write-Host "The specified text file contains the string '$string'."
} else {
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file does not contain the string 'Customers'.เราไม่พบ $string ในครั้งนี้ เนื่องจากเรามี ลูกค้า ไม่ใช่ ลูกค้า ใน file1.txt สิ่งนี้เกิดขึ้นเนื่องจากพารามิเตอร์ -CaseSensitive ซึ่งจับคู่แบบคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่และแสดงข้อความตามนั้น
สำหรับตัวอย่างโค้ดสองตัวอย่างข้างต้น -Pattern จะถือเป็นนิพจน์ทั่วไปเนื่องจากลักษณะการทำงานเริ่มต้น แต่เราไม่ได้ระบุ regex แต่เป็นสตริงแบบธรรมดา ดังนั้น ในกรณีนี้ เราต้องการให้ $string ถูกตีความว่าเป็นสตริงธรรมดา ในการทำเช่นนั้น เราสามารถใช้พารามิเตอร์ -SimpleMatch ดังต่อไปนี้
ใช้ Select-String กับ -SimpleMatch:
$path = "E:\Test\file1.txt"
$string = "customers"
$result = Select-String -Path $path -Pattern $string -SimpleMatch -Quiet
if ($result){
Write-Host "The specified text file contains the string '$string'."
}
else{
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file contains the string 'customers'.ตัวอย่างนี้คล้ายกับตัวอย่างก่อนหน้า แต่ที่นี่ เราใช้พารามิเตอร์ -SimpleMatch ซึ่งไม่ได้บังคับ แต่การใช้พารามิเตอร์นี้จะระบุว่า $string ในรูปแบบจะต้องไม่ถูกตีความว่าเป็นนิพจน์ทั่วไป
นอกจากนี้ เรายังใช้พารามิเตอร์ -Quiet เพื่อให้ Select-String ส่งกลับค่าบูลีน (True/False) แทนอ็อบเจ็กต์ MatchInfo ที่มีชื่อไฟล์ หมายเลขบรรทัด และทั้งบรรทัดที่ตรงกัน
ใช้ รับเนื้อหา Cmdlet
เราสามารถตรวจสอบว่าไฟล์มีสตริงหรือไม่โดยใช้ Get-Content cmdlet พร้อมด้วย Select-String cmdlet, Contains() method และ -จับคู่ โอเปอเรเตอร์ มาเรียนรู้แต่ละรายการด้านล่าง
ใช้ Get-Content กับ Select-String Cmdlet
ใช้ Get-Content กับ Select-String cmdlet เพื่อตรวจสอบว่าไฟล์มีสตริงที่ระบุใน PowerShell หรือไม่
ใช้รับเนื้อหากับ Select-String:
$path = "E:\Test\file1.txt"
$string = "customers"
$result = Get-Content $path | Select-String $string -Quiet
if ($result) {
Write-Host "The specified text file contains the string '$string'."
}
else{
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file contains the string 'customers'.เราได้เรียนรู้เกี่ยวกับตัวแปร $path, $string, $result, Select-String cmdlet และ < รหัส> - เงียบ พารามิเตอร์ สิ่งใหม่ที่เราใช้สำหรับตัวอย่างนี้คือ Get-Content cmdlet ซึ่งใช้ในการดึงเนื้อหาของรายการในตำแหน่งที่กำหนด เช่น เนื้อหาของฟังก์ชันหรือข้อความในไฟล์ข้อความ
ในขณะที่เรากำลังทำงานกับไฟล์ข้อความ สิ่งสำคัญคือต้องทราบว่า cmdlet นี้ทำงานอย่างไรกับไฟล์ข้อความ ขั้นแรก มันจะอ่านทีละบรรทัดจากไฟล์ข้อความที่กำหนดและส่งกลับคอลเลกชันของอ็อบเจ็กต์ โดยที่แต่ละบรรทัดจะระบุเนื้อหาหนึ่งบรรทัด หลังจากนั้น Select-String ค้นหารายการที่ตรงกันและส่งกลับ True หากพบรายการที่ตรงกัน สุดท้าย Select-String cmdlet ส่งกลับค่าบูลีนเนื่องจากเราใช้พารามิเตอร์ -Quiet ที่เราได้เรียนรู้มาก่อนหน้านี้
หากคุณใช้ PowerShell 3.0 คุณยังสามารถรับจำนวนบรรทัด
nจากจุดเริ่มต้น/สิ้นสุดของรายการที่ระบุได้
ใช้ Get-Content ด้วยเมธอด Contains()
ใช้ Get-Content ด้วยเมธอด Contains() เพื่อตรวจสอบว่าไฟล์มีสตริงที่ระบุใน PowerShell หรือไม่
ใช้ Get-Content ด้วยวิธีการประกอบด้วย():
$path = "E:\Test\file1.txt"
$string = "customers"
$fileContent = Get-Content $path
if ($fileContent.Contains($string)) {
Write-Host "The specified text file contains the string '$string'."
}
else{
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file contains the string 'customers'.เราได้เรียนรู้เกี่ยวกับ Get-Content ในตัวอย่างก่อนหน้านี้แล้ว มาขยายการสนทนานั้นโดยบอกว่า Get-Content cmdlet ดึงเนื้อหาของ file1.txt และส่งคืนเป็นวัตถุสตริง ตอนนี้เราสามารถใช้วิธีการที่ทำงานกับสตริงได้
หนึ่งในนั้นคือเมธอด Contains() ซึ่งเชื่อมโยงกับ $fileContent (วัตถุสตริง) ใช้ $string เป็นพารามิเตอร์ และตรวจสอบว่า $fileContent มี $string หรือไม่ หากเป็นเช่นนั้น นิพจน์ $fileContent.Contains($string) จะส่งกลับ True และทำให้เกิดการดำเนินการของบล็อก if มิฉะนั้น บล็อก else จะถูกดำเนินการ
โปรดจำไว้ว่า Contains() วิธีการจับคู่แบบคำนึงถึงขนาดตัวพิมพ์ ซึ่งหมายความว่า Customer และ customer เป็นค่าที่แตกต่างกันสองค่า ดูตัวอย่างต่อไปนี้
ใช้ Get-Content ด้วยวิธีการประกอบด้วย():
$path = "E:\Test\file1.txt"
$string = "Customers"
$fileContent = Get-Content $path
if ($fileContent.Contains($string)) {
Write-Host "The specified text file contains the string '$string'."
}
else{
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file does not contain the string 'Customers'.ใช้ Get-Content กับตัวดำเนินการ -match
ใช้ Get-Content กับตัวดำเนินการ -match เพื่อตรวจสอบว่าไฟล์มีสตริงที่ระบุใน PowerShell หรือไม่
ใช้ Get-Content กับ -match Operator:
$path = "E:\Test\file1.txt"
$string = "customers"
If (Get-Content $path | %{$_ -match "customers"}){
Write-Host "The specified text file contains the string '$string'."
}
else{
Write-Host "The specified text file does not contain the string '$string'."
}เอาท์พุท :
The specified text file contains the string 'customers'.ที่นี่ เราใช้ตรรกะเดียวกันแต่ใช้โอเปอเรเตอร์ต่างกัน -match จับคู่ทุกคำในข้อความ (ดึงข้อมูลโดยใช้ Get-Content) ด้วย $string. นิพจน์ที่อยู่ใน if ส่งคืน True หาก file1.txt มี $string; มิฉะนั้นจะส่งคืน False
ตัวดำเนินการ
-matchดำเนินการจับคู่โดยคำนึงถึงขนาดตัวพิมพ์ ดังนั้นcustomersจึงเหมือนกับCustomers
ใช้ Get-ChildItem Cmdlet
Get-ChildItem cmdlet ยังสามารถใช้กับ Select-String เพื่อตรวจสอบว่าไฟล์มีสตริงที่ระบุหรือไม่ นอกจากนี้ เรายังสามารถใช้ cmdlet นี้กับ Select-String และ ForEach เพื่อค้นหาอาร์เรย์ของสตริงในไฟล์ข้อความหลายไฟล์ มาดูกันว่าแต่ละวิธีทำงานอย่างไร
ใช้ Get-ChildItem Cmdlet กับ Select-String Cmdlet
ใช้ Get-ChildItem กับ Select-String cmdlet เพื่อตรวจสอบว่าไฟล์มีสตริงที่ระบุใน PowerShell หรือไม่
ใช้ Get-ChildItem กับ Select-String:
$path = "E:\Test\"
$string = "client"
Get-ChildItem -Path $path -Recurse | Select-String -Pattern $stringเอาท์พุท :
file2.txt:1:This is file2 and has some sample text for the client.
file4.txt:1:This is file2 and has some sample text for the client.ที่นี่ เราใช้ Get-ChildItem พร้อมด้วยพารามิเตอร์ -Path และ -Recurse เพื่อดึงข้อมูลรายการและรายการลูกทั้งหมดจาก $path ที่ระบุ . พารามิเตอร์ -Path ถูกใช้เพื่อกล่าวถึงเส้นทาง ในขณะที่ -Recurse ถูกใช้เพื่อรับรายการทั้งหมดใน $path ที่กำหนดและรายการย่อยทั้งหมด . ผลลัพธ์สำหรับ Get-ChildItem -Path $path -Recurse จะเป็นดังนี้:
เอาต์พุตสำหรับ Get-ChildItem:
Directory: E:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/7/2023 8:16 AM 60 file1.txt
-a---- 3/7/2023 8:16 AM 57 file2.txt
-a---- 3/7/2023 8:16 AM 90 file3.txt
-a---- 3/7/2023 8:16 AM 57 file4.txtผลลัพธ์ข้างต้นถูกส่งไปยัง Select-String cmdlet ซึ่งค้นหา $string ที่ระบุ และส่งคืนชื่อไฟล์ หมายเลขบรรทัด และทั้งบรรทัดที่มี $string ดังแสดงในผลลัพธ์ด้านบน
ใช้ Get-ChildItem กับ Select-String & ForEach
วิธีตรวจสอบว่าไฟล์มีสตริงหรือไม่:
- ใช้ตัวดำเนินการอาร์เรย์เพื่อสร้างอาร์เรย์ของสตริง
- หลังจากบันทึกเส้นทางในตัวแปรแล้ว ให้ใช้ลูป
ForEachเพื่อวนซ้ำอาร์เรย์ของสตริง ในการวนซ้ำแต่ละครั้ง:
- ใช้
Get-ChildItemเพื่อดึงไฟล์.txtทั้งหมดในเส้นทางที่ระบุ
- ใช้
- ใช้
Select-Stringcmdlet เพื่อค้นหาสตริงการค้นหาและเลือกเส้นทาง หมายเลขบรรทัด และคำที่ตรงกันโดยใช้ cmdletSelect
ใช้ Get-ChildItem กับ Select-String และ ForEach:
$searchStrings = @('customers','client')
$path = "E:\Test\"
Foreach ($ss in $searchStrings){
Get-Childitem -Path $path -Recurse -include "*.txt" |
Select-String -Pattern "$ss" |
Select Path,LineNumber,@{n='SearchWord';e={$ss}}
}เอาท์พุท :
Path LineNumber SearchWord
---- ---------- ----------
E:\Test\file1.txt 1 customers
E:\Test\file2.txt 1 client
E:\Test\file4.txt 1 clientเนื่องจากเราใช้ Select-String เพื่อค้นหาสตริงการค้นหา สตริงการค้นหาจะทำการค้นหาโดยคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ดูตัวอย่างต่อไปนี้
ใช้ Get-ChildItem กับ Select-String และ ForEach:
$searchStrings = @('Customers','CLIENT')
$path = "E:\Test\"
Foreach ($ss in $searchStrings){
Get-Childitem -Path $path -Recurse -include "*.txt" |
Select-String -Pattern "$ss" |
Select Path,LineNumber,@{n='SearchWord';e={$ss}}
}เอาท์พุท :
Path LineNumber SearchWord
---- ---------- ----------
E:\Test\file1.txt 1 Customers
E:\Test\file2.txt 1 CLIENT
E:\Test\file4.txt 1 CLIENTนั่นคือทั้งหมดที่เกี่ยวกับ PowerShell ตรวจสอบว่าไฟล์มีสตริงหรือไม่
