Monday, July 30, 2012

awk - 10 examples to read files with multiple delimiters

 In this article of awk series, we will see how to use awk to read or parse text or CSV files containing multiple delimiters or repeating delimiters. Also, we will discuss about some peculiar delimiters and how to handle them using awk.

Let us consider a sample file. This colon separated file contains item, purchase year and a set of prices separated by a semicolon.
$ cat file
Item1:2010:10;20;30
Item2:2012:12;29;19
Item3:2014:15;50;61

Monday, July 23, 2012

10 examples of paste command usage in Linux

   In this article, we will see how to use the paste command with some examples. paste command, by definition of man page, is used to merge lines of files. It is very useful for merging a single file and also for merging set of files as well. This article is divided into 2 parts:
  • paste command examples for single file handling
  • paste command examples for multiple files handling

Thursday, July 19, 2012

find command: 15 examples to exclude directories using prune

   When we search for files in Unix/Linux using the find command, we might want to search for files only in some specific directories, OR to search in all directories except a particular directory and so on.  prune option in the find command is the one for excluding a directory within a directory tree. In this article of find command, we will use how to use the prune option of the find command to exclude directories with some examples.

The simple find command below lists all the files and directories in the directory tree. The . indicates the path to search for the files. We will see the usage of prune using this directory tree:
$ find .
.
./area.pm
./C
./C/temp
./C/f2.c
./test.c
./temp
./temp/a.c
The functionality of prune is simple: "If the file is a directory, do not descend or traverse into it." Sounds simple, isn't it? Let us see some examples:

Tuesday, July 10, 2012

find2perl - 10 examples to find files without find command

    In our earlier articles on the find command, we discussed how to find files modified before X hours, X mins or X seconds. find command options mtime, mmin, newer, etc. are used to get the desired list of files. 

    How to find the files modified before X mins or X seconds in a system which does not support the 'newer' option or the 'mmin' options of the find command? 
     The answer for this find2perl. This is a tool which comes in-built along with perl installation. Hence, any Unix flavor in which perl is present should have the find2perl utility present in it. Perl has some modules for finding files which is as good as the Unix find. This utility creates a perl script which uses the File::Find module to find files, and gives an output which is nothing but a perl script equivalent of the Unix find command. On running this script, the desired list of files could be retrieved. 

Thursday, July 5, 2012

Read 2 files line by line alternatively

 In this article, we will see different ways in which we can read 2 files in parallel line by line, i.e, one line from file1 and one line from file2, next line from file 1, next line from file2, and so on. This way of reading produces a combined file output in which lines are alternated between the files.

Let us consider 2 sample files with the following contents:
$ cat file1
Unix
Linux
Solaris
$ cat file2
AIX
HPUX
Cygwin