Thursday, September 27, 2012

How to remove duplicate records from a file in Linux?

How to remove / delete duplicate records / lines from a file?

  Let us consider a file with the following content. The duplicate record is 'Linux' with 2 entries :
$ cat file
Unix
Linux
Solaris
AIX
Linux

Tuesday, September 18, 2012

grep vs awk : 10 examples of pattern search

"grep vs awk" does not mean comparison between awk and grep because there is no comparison to awk whatsoever from grep.  awk is a highly powerful programming language whereas grep is just a filtration tool. However, many of the things which is done using grep and few more commands can be done using a simple awk command. In this article, we will see the awk altenative for the frequently used grep commands.

Let us consisder a file with the following contents:
$ cat file
Unix
Linux
Solaris
AIX
Ubuntu
Unix

Thursday, September 13, 2012

$IGNOREEOF vs ignoreeof

How to prevent your terminal from getting closed OR prevent from getting the user logged out on pressing Control-D?
  The answer to this is IGNOREEOF / ignoreeof. Let us see what is this IGNOREEOF and the difference between IGNOREEOF and ignoreeof.

Control-D:
   Many of the times we might have pressed some key only to realize that the terminal got closed or he got logged out from his account. The user gets logged out whenever an EOF character is pressed which is Control-D.  So, if the user happens to be in his login shell, then logically the Ctrl-D ends up in getting the user's terminal closed as well.

Bash/Bourne shell:
    In Bash/Bourne shell, the log-out from the user account can be prevented by using the environment variable IGNOREEOF.

Wednesday, September 12, 2012

8 examples to find sum of all columns / numbers in a line

How to find the sum of all numbers or columns in a line of a text / CSV file?
    In an earlier article, we discussed about different ways of how to sum all the numbers in a file. In this, we will see how to find the sum of all the numbers in a line, or in other words, sum of all the columns in a line.

Let us consider a file with the following contents:
$ cat file
20 30 21
33 33 32
12 21 12

Monday, September 10, 2012

perl - 15 examples of substr command

substr is a perl function to extract substring from a string. substring in Perl is little special compared to other languages due to some additional functionalities. Let us discuss the substr function with examples.

Syntax:
 substr(STRING, OFFSET,  LENGTH, REPLACEMENT)
where STRING - the actual string from which the substring is to be extracted
          OFFSET - the starting position from where to extract
          LENGTH - the number of characters to be extracted from OFFSET (Optional)
          REPLACEMENT - the string to be replaced (Optional)

Thursday, September 6, 2012

ps command : what does the TIME indicate?

   One of the output fields of the ps commands is the TIME component. As per the man page of ps command, the TIME is: "CPU utilization of process or thread, incremented each time the system clock ticks and the process or thread is found to be running".  However, most of the times, when the user executes the ps command, he always sees the TIME as  00:00:00 as shown below(usually in development or practice environments). So, what exactly is the TIME? Let us see in this article.
$ ps
  PID TTY          TIME CMD
18315 pts/3    00:00:00 bash
18347 pts/3    00:00:00 ps
TIME:
   It is the total accumulated CPU utilization time for a particular process. The 00:00:00 against the bash process indicates no CPU time has been given at all for the bash process by the kernel till now. Is it confusing? How come the CPU utilization of the bash(login shell) is 0 when we are executing so many commands under bash?

Tuesday, September 4, 2012

8 examples to change the delimiter of a file in Linux

How to change the delimiter of a file from comma to colon?

  Let us consider a file with the following contents:
$ cat file
Unix,10,A
Linux,30,B
Solaris,40,C
HPUX,20,D
Ubuntu,50,E

Monday, September 3, 2012

What is a process in UNIX / Linux?

  A process is a program in execution in memory or in other words, an instance of a program in memory. Any program executed creates a process. A program can be a command, a shell script, or any binary executable or any application. However, not all commands end up in creating process, there are some exceptions. Similar to how a file created has properties associated with it, a process also has lots of properties associated to it.

Process attributes:
  A process has some properties associated to it: