Copy a specific line to a text file using PowerShell

Is it possible to count specific lines in a file and store the value in a variable?

For me it will be String "/ export" (without quotes).

+6
source share
2 answers

Here is one way:

$FileContent = Get-Content "YourFile.txt"
$Matches = Select-String -InputObject $FileContent -Pattern "/export" -AllMatches

$Matches.Matches.Count
+18
source

Here's how to do it.

$count = (get-content file1.txt | select-string -pattern "/export").length

As mentioned in the comments, this will return the number of lines containing the template, so if any line has more than one instance of the template, the count will be incorrect .

+7
source

Source: https://habr.com/ru/post/1584835/


All Articles