0

I have a text file containing tweets by users where:

470192   anneJackson    Apple's new iPhone is a no go for me
470193   trex90         For something so expensive, I'll go for Android anyway 
470194   anneJackson    What an outrageous pricing!

where the first column denotes an ID, followed by the username and user's tweet. What i want to do now is to write a command to count the number of tweets by each unique user which gives me something like:

anneJackson   2
trex          1

I tried something like:

cut -f 2 Twitter_Data_1 | sort | uniq -c

but it doesn't feel right as I'm only using the user column not including the tweet.

  • What is the delimiter of your text file? Is it one tab or some spaces? – glenn jackman Oct 17 '17 at 16:46
  • @glennjackman it would be tabs – Maxxx Oct 18 '17 at 0:34
3

It's a good job for awk:

awk '{ a[$2]++ }END{ for(i in a) printf "%-15s%s\n",i,a[i] }' Twitter_Data_1
  • a[$2]++ - increment occurrences of each unique username (presented by the 2nd field value $2)

The output:

trex90         1
anneJackson    2
  • yes awk works fine but I didn't really learn it in depth for now. If we were using cut, how would i modify the code above to yield the same result? – Maxxx Oct 17 '17 at 14:41
2

Assuming your file has fixed width columns and the spaces between columns are blanks and not tabs,

cut -c 10-24 Twitter_Data_1 | sort | uniq -c

This will give you

      2 anneJackson    
      1 trex90         

You don't need to include the tweet in your command as every line represents a tweet.

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.