Showing posts with label count. Show all posts
Showing posts with label count. Show all posts

Sunday, 23 July 2017

Count number of characters in file from command line and Vim

Count number of characters in file from command line and Vim


The command wc prints several file statistics: bytes, lines, words, etc. Using the -m option we can count the number of characters in a text file.

Install

Open a terminal window and run:

wc -m FILE

We can count the number of characters from Vim editor using this ex command:

:!wc -m %

References

wc(1) - Linux man page
{ Read More }


Sunday, 9 July 2017

COUNT column is fast in DB2

COUNT column is fast in DB2


Antonio Cangiano just set up a simple benchmark comparing COUNT(column) performance between untweaked DB2 Express-C and MySQL.

The results:
DB2 has very quick COUNT(column) performance
{ Read More }


Monday, 17 April 2017

Count Frequency Of Values In A Column Using Apache Pig

Count Frequency Of Values In A Column Using Apache Pig



There may be situations to count the occurence of a value in a field.
Let this be the sample input bag.


user_id course_name user_name
1 Social Anju
2 Maths Malu
1 English Anju
1 Maths Anju

Say we need to calculate no of occurence of each user_name.
Anju 3
Malu 1

Inorder to achieve this - COUNT Built In Function can be used.


COUNT Function in Apache Pig


COUNT function  compute the number of elements in a bag.
To group count a preceding GROUP BY statement and for global counts GROUP ALL statement is required.

The basic idea to do the above example is to group by user_name and count the tuples in the bag.


--count.pig

userAlias = LOAD /home/sreeveni/myfiles/pig/count.txt as
(user_id:long,course_name:chararray,user_name:chararray);
groupedByUser = group userAlias by user_name;
counted = FOREACH groupedByUser GENERATE group as user_name,COUNT(userAlias) as cnt;
result = FOREACH counted GENERATE user_name, cnt;
store result into /home/sreeveni/myfiles/pig/OUT/count;

The COUNT function ignores NULLs, that is tuple in the bag will not be counted if the first field in this tuple is NULL.
COUNT_STAR can be used to count fields including NULL values.




{ Read More }


Sunday, 16 April 2017

Count Chocula! I vant to eat your cereal!

Count Chocula! I vant to eat your cereal!


I swear.. I was like a seven year old again when I finally found these cereal boxes! I bought all of the Count Choculas and 2 Franken Berries, and Im sure Ill be back for more before they are gone for yet another year.


^My daughter and my Husband

Ive been looking everywhere for my beloved Monster Cereal!
When I saw them on the shelf, I let out one of my girly squeals. No shame.
Count Chocula has been my all time favorite cereal since I was a kid. Count Chocula first debuted in 1971, many years before I was born. I dont know a life without him, and I�m thankful for that.




Count Chocula and Franken Berry only return to supermarket shelves every September (or August as it seems this year!) and they only stick around through to Halloween.
It�s their limited nature that makes them so special. When Count Chocula was available all year, the cereal was easy to take for granted. To survive in the competitive breakfast arena, General Mills needed to pull out and take this route.

Because they only sell these during the Halloween season, people like myself actually hoard the stuff. I buy them by the boxes so that I can can keep enjoying it even after Halloween ends.



I love this fun vampire cereal so much! The dude you could always imagine kicking back with, and while recreationally pounding a little chocolate and milk. The delicious chocolately flavour frosted cereal with spooky fun marshmallows. And now I am going to go eat some. Byeeeee.

FUN FACT:

- In 1987, when Bela Lugosi was added to that year�s special edition box art. Several people of the Jewish faith weren�t happy that it looked like Dracula was wearing a Star of David necklace, feeling that it gave a bad name to their religion.
General Mills pulled the art, and replaced it with one that had the necklace airbrushed out. The original boxes are now quite valuable on the collectibles market.


^All 5 of the Monster Cereals
{ Read More }


Wednesday, 5 April 2017

Count the Occurence of All Words in a Number of Text Files

Count the Occurence of All Words in a Number of Text Files


Recently I was given the task to analyse a range of files (around 300) and count the occurrence of all the words in each file. So the aim was to put together a piece of code that goes through all files in a directory, reads in a file, lists all the words occurring in it and counting how many time each word has occurred.

I quickly found out that in case of a single file the process is rather simple, the following code does a fine job,

for w in `cat FILE.txt`; do echo $w;done|sort|uniq -c >> results.out
This code reads in FILE.txt and for each word in it counts its occurrence and the creates a list from it.

However putting this into a recursive script was a little more complicated. So I took another direction and found a piece of code using sed to do the same job on a single file. With this and some scripting knowledge I was able to put together just what I needed.

Additionally, I used the command basename to output the name of the file so I know which file was which.

The final piece of code looks like this,

for file in `ls /PATH/TO/DIRECTORY/`
do
basename /PATH/TO/DIRECTORY/FILE >>
results.out        sed s/ /n/g /PATH/TO/DIRECTORY/FILE | sort | uniq -c | sort -nr >>  results.out
echo "" >>  results.out
done

This does a perfect job and creates a single file with the output containing,
  • File name of each file
  • Occurrence of each word in the file, sorted from high to low
  • Empty line to separate data from each other
If anyone has any other suggestion or comment I am happy to hear it!



{ Read More }