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:

Thursday, August 30, 2012

getopts - How to pass command line options to shell script?

   In an earlier article, we discussed how to pass command line arguments to shell script and access them using positional parameters. In this, we will see how to use the getopts command to pass command line options to shell scripts. Command line options are the options or switches passed to a command. For example, -l, -r, -t are some examples of the command line options passed to the ls command.

Types:
  Command line options can be classified into 3 types:
  1. Options which does not take arguments:
    • ls -l    #no arg for -l
  2. Options which take arguments:
    • cut -d","  file #arg "," for -d
  3. Options + Additional command line arguments:
    • ls -l file1 file2 file3 
Syntax:
The syntax of the getopts command is:
 getopts optstring name
-> optstring - the string which contains the list of options expected in the command line
-> name - the variable name which is used to read the command line options one by one.

Tuesday, August 28, 2012

5 examples to extract substring in bash / ksh93 shell

Sub-string is nothing but extracting a part of a string. In an earlier article, we discussed the various ways of how to extract a sub-string from every line in a file. In this article, we will see how to extract sub-string exclusively in bash / ksh93 and its options. Moreover, this substring extraction is an internal command, and hence it is very good from performance perspective:

 The syntax of the sub-string extraction is:
${variable:offset:length}
where variable is the variable containing the string,
          offset specifies the position from where to start extracting the string,
          length specifies the length of characters to be extracted from the offset.

 Let us consider a variable containing a string "Solaris"
$ x="Solaris"

Monday, August 27, 2012

Insert new line or text after every n lines in Linux

In one of our earlier articles, we discussed how to insert a line before or after a pattern. In this article, we will see how to insert a line after every n lines in a file.

Let us consider a file. The requirement is to insert a line with text "LINE" afte every 2 lines:
$ cat file
Unix
Linux
Solaris
AIX
Linux