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.

Linux Ask!

Jul 292013
 

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

Answer:

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

1,2,3
2,3,4
3,4,5
...

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

Answer:

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.

e.g.

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

Jul 112013
 

Rewriting last Git commit

Answer:

Sometimes, when you regret your last commit and before it is pushed to remote, it is easy with the following commands:

git add file1.txt file2.txt
git commit -m 'Add some files'

# Now you regret to include file2.txt
git rm file2.txt
git commit --amend # Edit the commit message in the editor
Jun 162013
 

How to trace DNS from the root servers

Answer:

Use the command `dig`, e.g.

dig +trace mit.edu

;; global options: +cmd
.			19927	IN	NS	d.root-servers.net.
.			19927	IN	NS	e.root-servers.net.
.			19927	IN	NS	m.root-servers.net.
.			19927	IN	NS	l.root-servers.net.
.			19927	IN	NS	a.root-servers.net.
.			19927	IN	NS	k.root-servers.net.
.			19927	IN	NS	f.root-servers.net.
.			19927	IN	NS	j.root-servers.net.
.			19927	IN	NS	b.root-servers.net.
.			19927	IN	NS	h.root-servers.net.
.			19927	IN	NS	g.root-servers.net.
.			19927	IN	NS	i.root-servers.net.
.			19927	IN	NS	c.root-servers.net.
;; Received 228 bytes from 8.8.8.8#53(8.8.8.8) in 12 ms

edu.			172800	IN	NS	a.edu-servers.net.
edu.			172800	IN	NS	c.edu-servers.net.
edu.			172800	IN	NS	d.edu-servers.net.
edu.			172800	IN	NS	f.edu-servers.net.
edu.			172800	IN	NS	g.edu-servers.net.
edu.			172800	IN	NS	l.edu-servers.net.
;; Received 260 bytes from 199.7.83.42#53(199.7.83.42) in 223 ms

mit.edu.		172800	IN	NS	usw2.akam.net.
mit.edu.		172800	IN	NS	asia1.akam.net.
mit.edu.		172800	IN	NS	asia2.akam.net.
mit.edu.		172800	IN	NS	use2.akam.net.
mit.edu.		172800	IN	NS	ns1-37.akam.net.
mit.edu.		172800	IN	NS	ns1-173.akam.net.
mit.edu.		172800	IN	NS	eur5.akam.net.
mit.edu.		172800	IN	NS	use5.akam.net.
;; Received 404 bytes from 192.31.80.30#53(192.31.80.30) in 548 ms

mit.edu.		20	IN	A	23.46.134.151
;; Received 41 bytes from 63.150.131.81#53(63.150.131.81) in 4 ms