簡體   English   中英

需要一個groovy腳本的文本文件中精確匹配的字符串數

[英]Need count of exact matching string in a text file for a groovy script

我正在嘗試獲取一個名為groovy腳本的名為file.txt的文件中的完全匹配字符串。 所以假設我的文件是:

phone
phone
phone
iphone
phone
iphone

現在,當我運行時:-

grep -c "phone" file.txt

我得到的計數為6,而不是4,因為iphone還包含字符串phone。 在外殼中,我可以使用:-獲得確切的唯一字符串

grep -c '\bphone' file.txt

但這在常規外殼中不起作用。 如何使它工作?

您可以像這樣在groovy中做到這一點:

("file.txt" as File).readLines().count{ it=="AA" }

請注意,此操作非常急切,將加載整個文件。

如果這是一個問題,則這是一個惰性版本:

Files.lines(Paths.get("file.txt")).filter{ it=="AA" }.count()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM