Pages

Thursday, August 25, 2011

Fold files using fold command

  It is possible at times we need to read a file which is not organized in the normal way. We are used to reading files which is line sequential meaning every record is present in a line. Sometimes, it may so happen when you receive a file from a different platform, such as mainframes, the file may be organized as record oriented, meaning all the records will be in the same line.

  We need to process these files differently. Let us assume we have a file which is record oriented in which each record is of length 16.
$ cat file
Satya MainframesManoj Unix      Manish Windows
   As shown above, all the records are in the same line.


Unix has a command, fold, which will fold the lines and display.  Let us see how to use it:

1. Since our records are of length 16:
$ fold -w 16 file
Satya Mainframes
Manoj Unix
Manish Windows
  The -w option of the fold command is used to specify the width which in our case is the record length 16.

2. What happens if we do not use the -w option:
$ fold file
Satya MainframesManoj Unix      Manish Windows
   When the -w option is not specified, fold command takes the default width which is 80. Since all the records put together does not exceed 80 in our case, we got only one single line.

3. Let us create a file with record size 80 and check:
$ cat file
Satya Mainframes                                                                Manoj Unix                                                                      Manish Windows
Let us run the fold command on this file:
$ fold file
Satya Mainframes
Manoj Unix
Manish Windows
   As shown above, we got the records folded across individual lines.

Let us see in the next article the different ways to unfold the files.

No comments:

Post a Comment