I'm following a programming course and I'm trying to do a practice activity but I'm stuck. I have a file with the following list:
Monday day
Tuesday day
Easter holiday
Monday day
christmas holiday
Tuesday day
Friday day
Thursday day
thanksgiving holiday
What I'm trying to do is
- sorting this list,
- printing a new list containing only those names that are not repeated,
- counting the number of times each word appears in the list, and
- inserting a tab between the counter and the word.
This would be my desired output:
1 christmas holiday
1 Easter holiday
1 Friday day
2 Monday day
1 thanksgiving holiday
1 Thursday day
2 Tuesday day
I have tried using the following line of code:
cat my_file | sort | uniq -c | less
My problem is that words are not really sorted because words starting with capital letters would come before words starting with lowercase letters. Also, I don't know how to add the tab between the number and the word (in my output, there's only a space between them).
Could you help me?
sort -k1 file | uniq -c | column -tshould solve your problem. Here-k1arg tosortspecifies sorting based on first column