Tuesday, May 29, 2012

Right pad a string or a number with zero

 In an earlier article, we saw the different ways in which we can left pad a string with zeros. In this, let us see the different ways in which we can right pad a string or a number with zeros.

  Assuming a number, say 234, which we want to right pad to 7 digits with zeros:

1. This sed way had been discussed in example 4 of left padding a string as well. The same explanation holds good here. Only difference being instead of having a '0' before &, it is after &.
$ echo 234 | sed -e :a -e 's/^.\{1,6\}$/&0/;ta'
2340000
     One thing to note is the number 6(one less than actual, which is 7). This is because once the length of the number reaches 7, sed quits. To understand better, put 7 in place of 6 and check the result.

Monday, May 28, 2012

Shell - Read a text or CSV file and extract data

   In one of our earlier articles on awk, we saw how easily awk can parse a file and extract data from it. Shell also has properties with which we can handle text files: files with fields separated by white spaces or CSV files in which the fields are separated by a comma delimiter. Let us see in this article how to read and parse a file field by field or column by column and extract data from it using the while loop of shell. This article will also explain the usage of shell while loop.

Let us consider a file with sample contents as shown below. The file contains 3 fields: OS, the company and a random value.
$ cat file
Solaris Sun 25
Linux RedHat 30

Thursday, May 24, 2012

7 different ways to print the last line of a file in Linux

 In an earlier article, we saw the different ways in which we can print the first line of a file.  In this article, we will see the different ways in which we can print or display the last line or the trailer record of a file in Linux.

Let us consider a file with the following contents:
$ cat file
Unix
Solaris
Linux
1. The tail is the most common command used. tail command prints the last part of the files. -1 specifies to print one line from the last part.
$ tail -1 file
Linux

Tuesday, May 22, 2012

Different ways to reverse a string in Linux

 In this article, we will see the different ways in which we can reverse a string in Linux. Let us take a string "welcome" and try to reverse this string in different ways:

1. The Linux rev command is used to reverse the lines in a file. This command can take standard input as well as shown below.
$ echo welcome | rev
emoclew
Note: The rev command is not present in all flavors of Unix.

Monday, May 21, 2012

awk - Join or merge lines on finding a pattern

 In one of our earlier articles, we had discussed about joining all lines in a file and also joining every 2 lines in a file. In this article, we will see the how we can join lines based on a pattern or joining lines on encountering a pattern using awk or gawk.

Let us assume a file with the following contents. There is a line with START in-between. We have to join all the lines following the pattern START.
$ cat file
START
Unix
Linux
START
Solaris
Aix
SCO
1. Join the lines following the pattern START without any delimiter.
$ awk '/START/{if (NR!=1)print "";next}{printf $0}END{print "";}' file
UnixLinux
SolarisAixSCO
    Basically, what we are trying to do is:  Accumulate the lines following the START and print them on encountering the next START statement. /START/ searches for lines containing the pattern START.  The command within the {} will work only on lines containing the START pattern. Prints a blank line if the line is not the first line(NR!=1). Without this condition, a blank line will come in the very beginning of the output since it encounters a START in the beginning. 

Thursday, May 17, 2012

Different ways to print the next few lines after pattern match

  grep, awk  or a sed command is used to print the line matching a particular pattern. However,  at times, we need to print a few more lines following the lines matching the pattern. In this article, we will see the different ways in which we can get this done. The first part explains ways to print one line following the pattern along with the pattern, few examples to print only the line following the pattern and in the end we have ways to print multiple lines following the pattern.

  Let us consider a file with the following contents as shown below:
$ cat file
Unix
Linux
Solaris
AIX
SCO
1. The simplest is using the grep command. In GNU grep, there is an option -A which prints lines following the pattern.
$ grep -A1 Linux file
Linux
Solaris
  In the above example, -A1 will print one line following the pattern along with the line matching the pattern. To print 2 lines after the pattern, it is -A2.

Tuesday, May 15, 2012

GNU dates and strings conversion in Linux

 In one of our articles, we discussed about the GNU date command and the powerful options it has. In this article, we will see about the -d option in detail. -d option in date command can take multiple forms. Basically, it  takes a STRING and converts it to a date. And the STRING can be given in many different forms which makes the dates computation much easier. Let us see in detail with examples.

1. To convert a string "08-May-2012" to a UNIX date:
$ date -d "08-May-2012"
Tue May  8 00:00:00 IST 2012
     The output will look like as if the date command is fired on 8-May 2012. The -d option can take the string with - separated format as well. As shown , the string here takes the form  MM-Mon-YEAR format and gives the Unix date as output..

2. To format the above date output to a time stamp:
$ date -d "08-May-2012" '+%y%m%d'
120508
     %y gives YY, %m is number of the month and %d gives day.

Monday, May 14, 2012

awk - Match a pattern in a file in Linux

  In one of our earlier articles on awk series, we had seen the basic usage of awk or gawk. In this, we will see mainly how to search for a pattern in a file in awk. Searching pattern in the entire line or in a specific column.

  Let us consider a csv file with the following contents. The data in the csv file contains kind of expense report. Let us see how to use awk to filter data from the file.
$ cat file
Medicine,200
Grocery,500
Rent,900
Grocery,800
Medicine,600
1. To print only the records containing Rent:
$ awk '$0 ~ /Rent/{print}' file
Rent,900
     ~ is the symbol used for pattern matching.  The / / symbols are used to specify the pattern. The above line indicates: If the line($0) contains(~) the pattern Rent, print the line. 'print' statement by default prints the entire line. This is actually the simulation of grep command using awk.

Wednesday, May 2, 2012

Different ways to print first few characters of a string in Linux

  In this article, we will see the different ways in which we can extract and print the first 3 characters of every line in a file. Assume a file with sample contents as below:
$ cat file
Linux
Unix
Solaris 
1.  This is the best of all since its purely internal.  The file is read in the file loop and the first 3 characters are retrieved using the shell.
$ while read line
> do
>  echo ${line:0:3}
> done < file
Lin
Uni
Sol
   ${x:0:3} means to extract 3 characters from position 0 from the variable x. In this way, the shell can also be used to extract sub-string from a variable. This is one of the most important features of the shell.

2. Using cut, we can cut the first 3 characters.
$ cut -c -3 file
Lin
Uni
Sol
   The same can also be achieved by having 1-3 in place of -3.