Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Erzesel
2 years ago

Finding and counting given words in a text/text file is quite banal for Powershell.

Test.txt

 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

demo.ps1

 #lies Text aus einer Datei als ganzen String ein... (Zeilenumbrüche werden als Teil des Strings angesehen) $Text = Get-Content 'test.txt' -Raw #Suchbegriffe (jeweils ganze Wörter) "et" oder "magna" oder "dolor" $SuchPattern = '\bet\b|\bmagna\b|\bdolor\b' ($Text|Select-String -Pattern $SuchPattern -AllMatches).Matches | #finde die in $SuchPattern definierten Begriffe  Group-Object Value | #fasse gefundene Beriffe zu Gruppen zusammen  ForEach-Object {     #erzeuge für jede Gruppe ein Objekt mit den Propeties Wort und Anzahl    [PSCustomObject]@{     Wort  = $_.Name     Anzahl = $_.Count    }  }|  Format-Table pause

Directly creating an Excel worksheet might be a pain in the ass, and not just in Powershell. https://maliyaablog.wordpress.com/2017/10/02/how-to-createwrite-and-save-excel-using-powershell/

The easiest way is probably to export as a CSV file.

Importing a CSV file into Excel is cheap compared to the effort required to create it directly: https://www.pc-magazin.de/ratgeber/excel-csv-dateien-importieren-microsoft-office-3202344.html

This would look like this (I have combined the above functional script part into a short script and simply replaced Format-Table with the Export-CSV cmtlet)

demo.ps1

 $SuchPattern = '\bet\b|\bmagna\b|\bdolor\b' (gc 'test.txt' -Raw|Select-String $SuchPattern -a).Matches|group Value|%{[PSCustomObject]@{Wort=$_.Name;Anzahl=$_.Count}}|    Export-Csv -Path 'Wortzahl.csv' -Delimiter ';' -NoTypeInformation  #exportiere die Objekte in eine CSV-Datei für Exel (Exel  verwendet Semikolon als  StandardDelimiter) pause

Simply import the word count.csv file into Excel…

There are certainly some off-the-shelf programs that offer similar functionality. However, for me, such a thing is irrelevant. Writing such a Powershell script is faster than searching for any programs.

Note on the RegEx for the search term(s)

  • \b is the delimiter of a whole word
  • | means: or and separates multiple alternative search terms

$SuchPattern resolved:

  • \b word boundary
  • and the search term
  • \b word boundary
  • | or
  • \b word boundary
  • magna the search term
  • \b word boundary
  • | or
  • \b word boundary
  • and the search term
  • \b word boundary
Erzesel
1 year ago
Reply to  Chaostisi55

The onboard Powershell is the same (5.1.0) as under Win 10. That can't be the problem.

Doesn't work anymore is a pretty vague error description.

On the PowerShell side, nothing seems to have changed, apart from the fact that the security policies regarding script execution have been tightened (occasionally prompted to change the execution policies). However, this affects Windows 10 & 11.

However, this has no functional impact.

Regarding changes made when importing the CSV into Excel, I can't comment. Excel isn't my thing.

FaTech
2 years ago

Finding it will be difficult, I think… But writing… There is official documentation for it in various programming languages

safur
2 years ago

Notepad++ can count in the search function.

Do you mean in Excel with VBA?

Perhaps a formula would also be conceivable
https://www.computerbild.de/artikel/cb-Tipps-Software-Excel-Werte-zaehlen-31569615.html