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.