2

I have a list of files, 10 to be exact, in a recipe folder. I must determine the number of occurences of each word in all the files and then order them according to these frequencies. I can get my command to work on one file at a time, but I need it to work on each file at once and then output to one single file. For example: The word salt appears a total of 25 times overall, then output would be: 25 salt. So far I have this:

sed -d 's/\s/\n/g' < lyrics01.txt | sort | uniq -c | sort -nr >> lyrics01wordcount.tf

I need this to be altered to work on every file in the directory, I understand why it is working on one above. Any help would be much appreciated.

  • Change lyrics01.txt to *.txt to work on all .txt files. – αғsнιη Dec 1 '14 at 14:52
  • The -d option is not in any sed I can find, what does it do? – muru Dec 1 '14 at 15:05
2
sed -d 's/\s/\n/g' * | sort | uniq -c | sort -nr >> lyrics01wordcount.tf

Simple.

1

Two things:

  1. You don't need to use redirection with sed. It treats any filenames as input data. So sed ... lyrics01.txt works just as well as sed ... < lyrics01.txt (better, in fact, because...)
  2. Without redirection, you can specify multiple files to sed: sed ... file1 file2 ..., or, in your case, as KasiyA pointed out, sed ... *.txt.
  • Thanks a lot for the background info there, and the cool little quicker way to make it better. Very helpful my friend. – Demostroyer Dec 1 '14 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.