Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jul 292013

How to sum a particular column of a CSV file with awk


Assume you have a CSV file like the following (only contain number)


How do you sum the nth column of this file? It is easy with the use of awk

e.g. Assume you want to sum the 2nd column:

# awk -F "," '{ sum += $2 } END { print sum }' test.txt

Jul 252013

File content redirection without using cat


It is not uncommon that people have abused the command `cat` for many purpose, e.g. file I/O redirection

cat data.txt | sed 's/foo/bar/s'

You can avoid the cat command by the following commands:

data.txt < sed 's/foo/bar/s'
sed 's/foo/bar/s' < data.txt

Both directions are okay.

In fact, when you better understand the usage of some commands, you might find out that redirection is completely not needed.


sed 's/foo/bar/s' data.txt

Apr 042013

Constant in Ruby


In Ruby, a class constant has a name starting with an uppercase character, e.g.

Foo = 'bar'
puts Foo

Where you try to re-define the constant, a warning message will be shown, e.g.

Foo = 'bar'
Foo = '123'

(irb):18: warning: already initialized constant Foo
Mar 292013

Global and Local variable in Ruby


In Ruby, global variable has a name beginning with $, e.g.

$foo = 'bar'
puts $foo

In contrast, local variable has a name start with a lower case letter or an underscore character, e.g.

foo = 'bar'
puts foo