У меня есть файл вроде этого:
This is a file with many words.
Some of the words appear more than once.
Some of the words only appear one time.
Я хотел бы создать список из двух столбцов. Первый столбец показывает, какие слова появляются, второй столбец показывает, как часто они появляются, например:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
words3
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
- Чтобы сделать эту работу проще, перед обработкой списка я удалю все знаки препинания и изменю весь текст на строчные буквы.
- Если вокруг него нет простого решения,
words
иword
могут считаться двумя отдельными словами.
Пока у меня есть это:
sed -i "s/ /\n/g" ./file1.txt # put all words on a new line
while read line
do
count="$(grep -c $line file1.txt)"
echo $line"@"$count >> file2.txt # add word and frequency to file
done < ./file1.txt
sort -u -d # remove duplicate lines
По какой-то причине это только показывает "0" после каждого слова.
Как я могу сгенерировать список каждого слова, которое появляется в файле, вместе с информацией о частоте?