5 different command methods to get the sum of all numbers in a file

Let's say we have a sample file with the following content:
 5  
 7  
 8  

Here are the different unix command methods to get the sum of the file content:

1. Use awk

We just need to add the numbers respectively to the variable x then print out the result once the END of the file is reached
 awk '{x+=$0}END{print x}' file  



2. Use paste and bc

I've written an article about the 'bc' command before and here we can use paste and bc to do the task.
 paste -s -d"+" file | bc  

The "-s" option of the paste command joins all the lines of a file. The "-d" option is for choosing the delimiter with which the lines are to be joined. At the end, we will have "5+7+8" for the bc command to give the result.


3. Use perl

The way we use perl will be similar to that of 'awk'. With perl, we will use $_ to indicate the current line of the file, as $0 with awk. Once the end of the file is reached, the final value of the variable x will be printed:
 perl -lne '$x+=$_; END{print $x;}' file  


4. Use sed and bc

We here use sed just like paste. We will join all the lines and separate the numbers with "+" . So the sed command will give us "5+7+8" and the bc command will give us the needed result.
 sed -e :a -e 'N;s/\n/+/;ta' file | bc  


 
5. Use sed and echo

We can use the echo command to do the addition instead of the bc command. 
 echo $((`sed '$!s/$/+/' file | tr '\n' ' '`))  
We use the sed command to add a "+" at the end of each line, we also use the 'tr' command to remove each new line and the echo command will do the final task of addition.


About the author: Neil is a Linux aficionado and SEO expert. He is currently running Hosting Tavern, a website to review hosting companies. You can check his website if you are looking for arvixe coupon.

Check PiCE, a rain resistant case for Raspberry Pi

PiCE is a new case for Raspberry Pi. The project is currently raising fund on KickStarter.


What makes PiCE special is its solid design which can protect the tiny computer from many things, even the rain.

At first glimpse PiCE looks like a big removable hard drive. It looks very sturdy and solid. And since the case is made of metal, mostly Zinc, you will never have to worry about your Pi getting damaged or broken. So PiCE is perhaps a great choice for you if you carry your Raspberry Pi around frequently.
 
Another great thing is that under the protect of PiCE, you can play with Raspberry Pi outside, even under the rain. However, it does not mean the case is water resistant. If you soak your PiCE under water, I believe something bad will definitely happen.

PiCE can protect Raspberry Pi under rain

Just like other cases for Raspberry Pi, PiCE is designed to allow you access all the ports easily and conveniently. PiCE also has the led pipes for you to check the status of your tiny computer.

PiCE also has a great heat dissipating power. The heat from the Raspberry Pi will be dissipated through 3 columns to come out the Zinc case. This is somehow similar to the metal case of your desktop computer acting like a passive heat dissipating system.

3 columns for heat dissipation

After all, PiCE seems like a great case for Raspberry Pi. The design looks great and the silver color is awesome.

So if you are interested in PiCE, you can read more about it on KickStarter. And if you are generous, you can consider helping the project meet its fund raising goal.

Top 5 Android apps to help you lose fat

If you are trying to lose a few kilos, dieting and exercising are what you need to do. However, while it seems to be simple, you may dont have any idea about what foods to eat, what exercises to do and you also lose the motivation and determination easily. So here are 5 Android apps that can give you some help to make your fat loss journey easier and more convenient.

MyFitnessPal

MyFitnessPal is one of the best reviewed fat loss apps on Google Play, it has a huge database of millions of foods that it can count calories for. It also has a list of hundreds of exercises for you to try. A nice feature of MyFitnessPal is that the app syncs with the service's website to match food to the database. And with MyFitnessPal, you can connect with your friends to diet, exercise and lose weight together.


Click here to download MyFitnessPal on Google Play


Superfoods

Most of us have heard about these Superfoods and know that we should eat them frequently. But not many people know specifically what these foods really offer, how to prepare them, how to cook and how much to eat.

Superfoods will therefore come handy for you as it gives you a series of many mini articles about the superfoods you should eat. This app gives you all the facts about the foods and leaves you with all the knowledge to get the most benefits from the superfoods.


Click here to download Superfoods on Google Play

Lose it

Lose it is a free app that let you search for calories and nutritional info of foods and allows you to track you exercise routine. You can use Lose I to calculate how many calories to eat per day and how long it should take you to reach your desired goal, all you need to do is to input your current weight and the goal weight. Lose it also allows you to join a support community where similar people encourage each other to lose fat. And the best thing I love about Lose it is that with this app, you can use your mobile device to scan the barcodes of any item and Lose it will give you the nutritional and calorie info from a huge online databse.


Click here to download Lose it on Google Play

My Diet Coach

This app is quite different, it does not give you anything about exercise routine, diet program... it simply encourages and motivates you to lose fat. My Diet Coach mostly targets the female users. It is all about motivating the user to be persistent to their fat loss plan. 

 With My Diet Coach, you can set your own reminders, such as “ one more set” or “drink water” … with motivational photos and slogans to help you combat laziness and food temptation. 

Click here to download My Diet Coach on Google Play

WeightWatchers Mobile App

If you have done some researches about fat loss diets, you have probably heard about WeightWatcher already. It is one of the most popular diet plans. And this Android app is dedicated to the diet plan. The app provides a huge library of foods, including restaurant meals, and gives a WeightWatchers point value for each entry, as well as calculating your daily point allowance and how many points you have remaining.


Click here to download WeightWatchers Mobile App on Google Play.

About the author: Alice is an expert in fat loss, dieting and exercising with over 10 years of experience. She is currently writing reviews for fat burning supplements.

Tip to write scripts: Internal vs External commands

There are actually two types of Unix commands, internal and external. This concept may be new to new Unix and Linux users, but knowing this will help you write better scripts.

Here are how internal and external commands are defined:

Internal commands are the built in commands of the shell. Which means that when you execute an internal command, no process will be launched to execute the command. Therefore the speed of executing an internal command will be very high. An example for an internal command is 'cd', when you use 'cd' to change the directory, no process is created, the directory is just simply changed.

Some other examples for internal commands: source, fg, bg ...

External commands are executable scripts in separate files. So when you want to execute an external command, a script will be run. For example, when you want to use 'perl', the file /usr/bin/perl will be executed.

Some other examples for external commands: sed, mv, awk, cat ...

To know whether command is internal or external, you can use the following command:
 type command  

And if the command is internal, the output will say that the command is shell builtin. If the command is external, the output will give you the path to the command. For example:

difference between internal and external unix commands

As a matter of fact, most of us rarely care about internal and external commands because we always have to use both types of command when writing scripts. However, if you know which command is internal or external, you can improve the performance of your scripts, because as said above, the executing speed of an internal command is always faster. Its obviously hard to feel the difference of speed with just a single command but if you have a big script, the less external commands you use, the less bulking your script will become.

Of course, we dont usually get the option to choose an internal over an external command. However, a careful look at our scripting practices, we might find quite a few places where we can avoid external commands.

 For example, in a script that you need to do some math problem. Lets say to add 2 numbers. You can use either:
 n=`expr $i+$j`  

or:
 let n=i+j  

The difference between the two is that 'let' is an internal command and 'expr' is an external one. So executing 'expr' will be slower than 'let'. Thats why its recommended to use 'let' instead of 'expr' in a big script.

5 best Android alarm clocks

With the popularity of smartphones, the need for a regular alarm clock has been declined. All we need is just a simple app to turn your phone into an alarm clock. Although every Android phone always comes with a default alarm clock app, there are many other options for you to choose. Here in this article, I will give you a list of the apps I know so you can choose the best android alarm clock for yourself.

The following alarm clocks for Android dont just beep tediously around to make you wake up. With these apps, you can set up the sound that you love and force yourself to solve a complex math problem the moment you open your eyes. So no matter what kind of sleeper you are, with one of these android alarm clocks, you will always wake up on time.

Alarm Clock Xtreme

Perhaps the best Android alarm clock in my opinion, I always used this app whenever I needed to wake up earlier than I used to. And this app never failed me. The creator of Alarm Clock Xtreme understands that at the waking up moment, everybody is lazy and does not have the mind to do any complicated task. So in order to make you totally wake up, this app forces you to solve some math problems or makes you enter some captchas the moment you wake up. This activates your brain and you don't feel as sleepy anymore.

And the snooze button of Alarm Clock Xtreme is also a little bit different from that of the normal alarm clock apps. Instead of just hitting the snooze button, you can complicate things up, like setting that you need to shake the phone or solve a puzzle to snoozing the alarm.

best android alarm clock

Click here to download Alarm Clock Xtreme

Alarm Clock Plus

This is another good choice for an alarm clock for Android. Alarm Clock Plys comes with common alarming features like shake-to-snooze, volume fade-in, and repeating alarms. And in case these usual features are not enough to wake you up, you can set the app to play a specific music track when you wake up or even flash the flashlight if you have wake up very early in the morning. And just like Alarm Clock Xtreme, you also have to solve math problems to snooze with Alarm Clock Plus. Another cool feature is that you are able to set alarms for naps, it is similar to a countdown timer thus making it easier to set an alarm if you're taking a short siesta.

the best android alarm clock

Click here to download Alarm Clock Plus

Wave Alarm

With this app, you will need to wave yourself over in front of the front-facing camera on your phone or tablet to snooze or dismiss alarms. Perhaps the best Android alarm clock for those who love to take self portraits. One thing I love about Wave Alarm is that besides the alarming features, it has a very beautiful, stylish normal clock. So if you just like the clock widget, you can completely turn off all the features if you happen to just like the clock face.

best alarm clock for android

Click here to download Wave Alarm

DoubleTwist Alarm Clock

You may have known the DoubleTwist team from their music apps, and their Alarm clock app is awesome too. And just like Wave Alarm, DoubleTwist Alarm Clock has fantastic clock faces, both the digital and analog versions look extremely beautiful. DoubleTwist Alarm Clock has all the features you need for an alarm clock, you can set alarms manually, based on sleep cycles, based on sunrise, or just short naps. There are also other features like multiple alarms, recurring alarms, and the ability to set custom labels to your alarms. Overall, it is an effective yet beautiful android alarm clock that does what it says.

android alarm clock

alarm clock for android

Click here to download DoubleTwist Alarm Clock

Alarm Clock Free

This app has an elegant digital clock interface. With Alarm Clock Free installed, you'll be able to use your Android phone as a bedside clock that displays the time using a gorgeous-looking green LED. And to wake you up, this app has several features like multiple alarms, fade-in sounds, and super-sized alarm clock buttons. One feature that I like about Alarm Clock Free is that it also manages to wake you up even if you set your Android phone in silent mode.

android best alarm clock

Click here to download Alarm Clock Free

Different command methods to count the total lines in a file

In this article, we will see different methods to get the count of the total lines in a file.



1. Use the combination of wc and cat

This is the simplest method, a piping of cat and wc
 cat file | wc -l  

2. Use grep

grep has the '-c' option to count lines when some patterns are matched. To get the pattern of every line, we will use '.*', which means to match anything.
 grep -c '.*' file  

3. Use sed

The sed command uses the '=' operator for line numbering. The '$' tells to give the count only for the last line.
 sed -n '$=' file  

4. Use awk

With awek, we can use the NR variable to get the lines count. NR contains the line number and by printing NR at the end block, we get the line number of the last line, which is exactly what we need:
 awk 'END {print NR}' file  

5. Use perl

Similar to awk, perl also uses the END block. But instead of NR, perl uses $ to denote the line number.
 perl -lne 'END {print $.}' file