I want to write a shell script which creates automatically 26 dictionary files, where the first file should contain all the words starting with a or A, the second all the words starting with b or B, … etc. Where each dictionary file is sorted. For example, if I had a file that had the words Lime, Apple, Orange, Avacado, Apricot, Lemon. Then I want a new file that contains in order Apple, Apricot, Avacado, a file that contains just Orange, and a file that contains Lemon, Lime.
I thought about doing this using sort, so it could be:
sort sample.txt
but that would not put each section of words into a new file. So I thought of doing:
sort sample.txt > [a-z].txt
but that just makes one new file titled [a-z].txt
How do I make different alphabetically sorted files from the list of words in the file? I want it to be like a.txt, b.txt, etc with each containing all the words that start with that letter.
Advertisement
Answer
You can do this with awk:
awk '{ print $0 >> toupper(substr($0,1,1))"_wordsfile" }' <(sort wordsfilemaster)
Where wordsfilemaster contains the original dictionary file, run sort on the file and redirect the output back into awk. Append the line to a file generated by taking the first character of the line, converting it to upper case and then appending “_wordsfile” e.g. files get created as A_wordsfile or O_wordsfile.