Wednesday, November 28, 2012

25 examples of vi / vim substitution commands

 In this article, we will see the commonly used vi substitution commands. All the below commands are to be given in escape mode:

Common tricks:
1. To replace the 1st occurrence of 'a' with 'A' in every line:
2. To replace the all the occurences of 'a' with 'A'
g is for global substitution, ie. substitution for all matches.
3. To replace 'e' with 'E' only in the current line:

Monday, November 26, 2012

awk - 10 examples to insert / remove / update fields of a CSV file

How to manipulate a text / CSV file using awk/gawk? How to insert/add a column between columns, remove columns, or to update a particular column? Let us discuss in this article.

Consider a CSV file with the following contents:
$ cat file
1. To insert a new column (say serial number) before the 1st column
$ awk -F, '{$1=++i FS $1;}1' OFS=, file
$1=++i FS $1 => Space is used to concatenate columns in awk. This expression concatenates a new field(++i) with the 1st field along with the delimiter(FS), and assigns it back to the 1st field($1). FS contains the file delimiter.

Thursday, November 22, 2012

7 examples of find command with xargs in Linux

 find command becomes very powerful when used with xargs. xargs is a beautiful command which actually builds a command and executes it. Though xargs command can be used standalone to work on standard input or on a file, it becomes all the more powerful when used with the find command. The man page definition of xargs is :"build and execute command lines from standard input"

Let us see some examples of find command with xargs to understand more:

Monday, November 19, 2012

How to swap 2 columns in a file in Linux?

How to swap 2 columns or fields in a text or a CSV file?

Let us consider a file with the following contents. The requirement is to swap the first 2 fields:
$ cat file
1. Using awk:
$ awk -F, '{print $2,$1,$3}' OFS=, file
Using the print statement, simply the fields can be printed in the order in which it is needed.  -F is to specify the input field separator and OFS for the output field separator. However, this method will not be ideal for a file with large number of fields since it forces us to print all columns individually.

Wednesday, November 7, 2012

How to retrieve or extract the tag value from XML in Linux?

How to fetch the tag value for a given tag from a simple XML file?
  Let us consider a simple XML file, cust.xml, with  customer details as below:
<?xml version="1.0" encoding="ISO-8859-1"?>
The requirement is to retrieve the tag value of "CustName" from the xml file. Let us see how to get this done in different ways:

1. Using the sed command:
$ sed -n '/CustName/{s/.*<CustName>//;s/<\/CustName.*//;p;}' cust.xml
Using sed substitution command, the pattern matching till the beginning of the opening tag is deleted. Another substitution is done to remove the pattern from the closing tag till the end. After this, only the tag value will be left in the pattern space.

Monday, November 5, 2012

Perl - User defined function to simulate uc and lc

  uc and lc are Perl functions to convert strings into upper-case and lower-case respectively. In case, if the user wants to simulate the uc and lc functions by writing his own function, how to do it? Let us discuss in this article the different methods of how to simulate the uc and lc function?

Method 1:

  3 things should be known before going ahead with this one:

 1. The decimal values ranges of alphabets are:
       A - Z : 65 to 90
       a - z : 97 to 122
 2.  ord - Perl function which gives the decimal value of an ASCII character.
 3.  chr - Perl function which gives the ASCII character for a given value.