Skip to content
September 17, 2010 / Nandha kumar

Internship session on 11 & 12 September

Hi to all,
This is the second week I am attending the internship program conducted by Chandrashekhar Babu sir.
The saturday session [ Javascript ] was cancelled due to the celebration of Ganesh Chathurthy Function.
On sunday , sir taught us about Linux commands.
First He started with the “Sed” command and told about few more of its features.
He also told about to make the Sed command work equivalent to other commands like cat,…
By adding an option -e with the sed command we can include the regular expression functionality to it. -e stands for expression.
Few examples on sed command:
$ sed -e filename.txt
simply prints the entire contents of the file just like cat command.
$ sed -e ‘p’ filename.txt
prints each line in the file twice.
$ sed -e ‘1p’ filename.txt
prints only the first line of the file twice.
$ ls -l | sed -e ‘5a\’
insert a new line after the 5th line in the output of ls command.
$ ls -l | sed -e ‘s/nandha/root/’
replaces the first occurence of the string “nandha” with that of the string “root” in
the output of ls command.
$ ls -l | sed -e ‘s/nandha/root/g’
This command replaces the string “nandha” with “root” globally.
Here denotions are
a – append
s – substitute
c – change
d – delete
p – print
g – global
$ ls -l | sed -e ‘s/[0-9]/x’
Replace the numbers with the single character ‘x’
$ ls -l | sed -e ‘/^d/d’
To list only files and not the directories.
$ ls -l | sed -e ‘/test/d’
Delete the lines of output that contains the word “test”.
$ ls -l | sed -e ‘s/ \+/ /g’
Replaces multiple occurence of blank spaces with a single blank space of ls output.
$ ls -l | sed -e ‘s/ \+/ /g’ | cut -f5 -d’ ‘
Produces the same output as previous command put prints only first 5 lines. Here -f
is the field number [ column] and -d is the delimiter. The delimiter used is blank space.
Then Sir taught about the head, tail, history commands.
$ head -5 fruits.txt
extracts first 5 lines from the file
$ tail -5 fruits.txt
outputs last 5 lines from the file.
$ nl fruits.txt | head -10 | tail -5
prints the lines from 6-10 of the file. here nl command is used to include line
numbers in the output.
$ nl fruits.txt | head -6 | tail -1
prints only the sixth line of the file
History command is one important command which stores everything that we type on the shell for several days.We can store all those commands that we typed into a file for verifications too.
$ history
$ history > $(date+”%Y%m%d-%H%M%S.txt”)
This command directs the output of the history command into a new textfile with current date and time as its filename.
Here Y – year
m – month
d – day
H – hours
M – Minutes
S – Seconds
Then Sir taught about the most interesting programming language – Awk. He told about the power of Awk and the places where to use it. Awk can be used as a substitute for several commands like grep, find,
but Awk is not a preferred to be used as a programming language since achieving tasks with it, is more complex. Instead we can use it for shell scripting. We can use languages like Perl, Python as replacement for Awk. Awk also has support for Regular Expressions.
I will try to putup a post more detailedly about Awk later.
Few more commands Sir taught us are,
$ ps -aeo pid,rss,cmd
Prints the amount of RAM used by each running process in our system. here rss – resident set size
We can save the Awk commands as file with .awk extension. Then we can execute that file using the following command
$ awk -f filename.awk
$ hdparm
prints detailed information about our Harddisk
$ hdparm -i
$ hdparm -I
$ hdparm -t /dev/sda
performs read tests on the harddisk [ /dev/sda ] and prints the transfer rate of data per second.
Finally Sir asked about the answer for the task He gave last week. The task is to pick the valid time correctly from the list of time given in a file. The command is
$ egrep ‘\b([0-1][0-9]|[2[0-3]):([0-5][0-9]):([0-5][0-9])\b’



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: