簡體   English   中英

從 bash 中的文本文件中提取所有單詞

[英]Extract all the words from a text file in bash

我需要將文件中的所有單詞讀取到變量中。 除此之外,我只需要存儲每個單詞一次。 選擇不會對鍵敏感,因此“ Hello ”、“ hello ”、“ hElLo ”和“ HELLO ”將算作同一個詞。 如果一個詞有撇號,比如“ it's ”這個詞,它必須忽略“ s ”,只將“ it ”算作一個詞。

為此,我使用了以下命令:

#Stores the words of the file without duplicates
WORDS=`grep -o -E '\w+' $1 | sort -u -f`

前兩個條件得到滿足,但這種方法將“ it's ”之類的詞計為兩個單獨的詞“ it ”和“ s ”。

也許,像這樣:

WORDS=$(grep -o -E "(\w|')+" words.txt | sed -e "s/'.*\$//" | sort -u -f)

暫無
暫無

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

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