Monday, September 26, 2011

5 different ways to add all the numbers in a file

In this article, we will see the different ways in which can add or sum up all the contents in a file.

Let us take a sample file with the following contents:
$ cat file

Friday, September 23, 2011

5 useful settings to have in .vimrc for a developer

  In this article, we are going to see some of the most important settings which is mandatory for a developer to have in his .vimrc or .exrc file.

set incsearch : When we want to search for something in vi, we put a slash(/) and start typing the word in the escape mode.  On finishing typing, and when we press enter, we get to know whether  the word is present in the file or not.  The setting ‘incsearch’ is a special feature in which you will know whether what you are typing is there or not in the file as you type it. As you keep tying, the matching contents keep getting highlighted. It is like in google search, we get to see the search results as we keep tying.

Wednesday, September 21, 2011

sed - Selective printing

    In this sed article, we are going to see the different options sed provides to selectively print contents in a file.  Let us take a sample file with the following contents:
$ cat file
Gmail 10
Yahoo 20
Redif 18

Tuesday, September 13, 2011

awk - Passing arguments or shell variables to awk

  In one of our earlier articles, we saw how to read a file in awk. At times, we might have some requirements wherein we need to pass some arguments to the awk program or to access a shell variable or an environment variable inside awk. Let us see in this article how to pass and access arguments in awk:

Let us take a sample file with contents, and a variable "x":
$ cat file1
$ echo $x
Now, say we want to add every value with the shell variable x.

Tuesday, August 30, 2011

5 different ways to join all lines in a file

  In one of our earlier articles, we saw how to fold a file using the fold command. In this article, we will see the different ways in which we can unfold the file, or in other words, different ways to join all the contents of the file in one single line.

Let us take a file with the following contents. The file contains user's name and user expertise. Every line in the file is of length 16. The below shown examples are different ways of how to unfold the file contents:
$ cat file
Badri Mainframes
Suresh Unix
Rajendar Clist
Sreedhar Filenet

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.

Thursday, August 18, 2011

A Shell Script to do shell scripting faster

   Shell Scripting is all about automating a particular task, doing a task a bit faster than what it takes to do manually. However, when we write a shell script, if we notice properly, the amount of time we take to get it done is little more. In other words, can we think of ways in which we can write and test a shell script faster?

  Typically, when we write a shell script, we open the editor, write something, save and close it. Run the script. Depending on the output or some error thrown, open the file again, edit something, save and close it. Run the script, and this goes on and on and on. The focus here is the amount of time you spent on saving the file every time, running it, and again opening the file and repeating this whole process umpteen times. In some cases, while running, we get error, and by the time we re-open the file, we forget what the error is. Now, again run the script and see the error again and re-open it. The amount of time we spend here is pretty high.

Thursday, July 28, 2011

What is a profile file?

    A profile file is a start-up file of an UNIX user, like the autoexec.bat file of DOS. When a UNIX user tries to login to his account, the operating system executes a lot of system files to set up the user account before returning the prompt to the user.

    In addition to the system settings, the user might wish to have some specific settings for his own account. To achieve this in UNIX, at the end of the login process, the operating system executes a file at the user level, if present. This file is called profile file.

Monday, July 25, 2011

5 important things to follow to become good in shell scripting

       Everybody wants solutions to their problems, and more importantly,  we are now expected to get it faster. One can give faster solutions in Unix when one has a good grip on the command line options, and more importantly scripting languages. There are quite a few of them present: Basic Shell scripting, perl scripting, python, ruby, etc. Though our title reads to become good in shell scripting, we in general are going to discuss how to become good in automating things? Or the kind of outlook one needs to have to go in that direction?

    How can one become good in automation? Is it just books one has to read and master or something more? Let us see in this article 5 different traits if one has can turn things around:

Monday, July 4, 2011

Difference between $x and ${x}

   Many times in shell scripts we would have witnessed two different forms of notations being used against variables: $x and ${x}.  Are they both same or is there any difference between them? Let us discuss in this article:

Let us assign a value to the variable, x:
$ x=10

Thursday, June 30, 2011

3 different ways of dumping hex contents of a file

 At times, when you are doing any conversion of ascii to hex or to octal, we would like to view the hex contents or the hex dump of the file, be it ascii or binary. Let us see in this article the different ways to do it:

Let us contain a sample file, say file1, with the following content:
$ cat file1

Tuesday, June 28, 2011

3 different time stamps associated with a file in Unix

  Any file present or created in Unix has 3 different time stamps associated with it . The 3 different timings are:

1. Last modified time - This refers to the timing when the file has been modified last. For a newly created file, this refers to the time when the file is created.

2. Last accessed time -  This refers to the timing when the file has been accessed last. Access can be either when the file is read, or the file is written to or even if the file has been executed. The last accessed time gets updated when any of the above file operations is being done on a file.

3. Last changed time - This refers to the timing when the file status has been changed last. The change in file status could be due to any of the following reasons:
   i) File ownership being changed.
  ii) Group to which the file belongs to is changed.
 iii) File permissions such as read, write or execute permissions has been applied or removed from the file.
 iv) Advanced file permissions such as SUID, SGID or Sticky bit has been applied or removed on the file.
  v) When the file content is updated.

Tuesday, June 21, 2011

Inode - All about inodes

  Everything is a file in Unix, we say.  Users deal with files a lot, however, we usually can get most of our work done without needing to know about i-nodes, and their relation with files. However, if we know it, our clarity on looking at the file system will get better.

1. What is an Inode?
     Inodes in Unix are data structures which contain all the properties of a file, metadata. The properties of the file include file size, file owner, the group to which the file belongs to, file access rights, hard link count, the location where the file contents are present and time stamps(last modified time, last accessed time, last changed time). In other words, the inode data structure contains all the information of the file except the file name and its contents.

Wednesday, June 15, 2011

5 different ways to do numbering of a file contents

  Sometimes, we might have a requirement wherein we need to display the file contents with its line number. In this article, we will see the different ways in which we can achieve this line numbering of file.

Let us take a sample file, say file1, with the following contents:

Tuesday, May 31, 2011

cut - cut files with a delimiter

   cut is a very frequently used command for file parsing. It is very useful in splitting columns on files with or without delimiter. In this article, we will see how to use the cut command on files having a delimiter.

Let us consider a sample file, say file1, with a comma as delimiter as shown below:
$ cat file1
Niti,Mother,30,Group Lead
 The first column indicates name, second relationship, the third being the age, and the last one is their profession.

Tuesday, May 24, 2011

awk - Read a file and split the contents

 awk is one of the most powerful utilities used in the unix world. Whenever it comes to text parsing, sed and awk do some unbelievable things. In this first article on awk, we will see the basic usage of awk.

The syntax of awk is:

awk 'pattern{action}' file

   where the pattern indicates the pattern or the condition on which the action is to be executed for every line matching the pattern. In case of a pattern not being present, the action will be executed for every line of the file. In case of the action part not being present, the default action of printing the line will be done. Let us see some examples:

Tuesday, May 17, 2011

bc - Unix calculator

  Whenever we need to do some calculation, we open the calculator in the windows. For unix programmers, why to go for windows calculator when we have an in-built one, bc.  bc is a unix command which stands for bench calcultor. Depending on the way we use it, it can either look like a programming language or as a interactive mathematic shell. Let us see in this article how to use bc  to do some simple arithmetic.

   bc can be invoked either as a pipe of other command OR by getting into the interactive mode. Let us start by seeing it from the command line perspective.

Tuesday, April 5, 2011

sed - Read from a file or write into a file

  In this sed article, we will see how to read a file into a sed output, and also how to write a section of a file content to a different file.

  Let us assume we have 2 files, file1 and file2 with the following content:
$ cat file1
$ cat file2
  sed has 2 options for reading and writing:

Monday, March 28, 2011

Different ways to add header and trailer line to a file

   In this article, we will see the different ways to add a header record or a trailer record to a file.

Let us consider a file, file1.
$ cat file1

Thursday, March 17, 2011

Different ways to delete ^M character in a file

 Control-M is a character found at the end of a line usually in files transferred from windows. Before, processing these files in UNIX, we need to remove the ^M characters. Let us see in this article, the different ways to delete the Control-M from the files:

 Consider a file, file1, which has the control M characters:
$ cat -v file1

Tuesday, March 15, 2011

comm - The beautiful comparison

 comm is one of the most underused commands in UNIX. I have seen many times programmers resorting to writing a shell script to achieve something which comm could have done it easily. Let us see how to use this comm command.

 Say, you have a file, file1, which contains the list of files present in a version. And file2 contains the list of files received in the next version:

Thursday, March 10, 2011

Oracle 10g Express: How to set up Oracle XE?

  Oracle XE is a Oracle Express edition Oracle software provided by Oracle. This software allows developers to install Oracle in their computer and practice Oracle and PL/SQL.

Steps for Installation:
1. Download the Oracle Database 10g Express Edition software fromOracle XE.
2. Install the software just like any windows software.
3. During Installation, it will ask you for password. Remember the credentials. The username is SYS.

Tuesday, March 8, 2011

Difference between $* and $@ in Shell

 In one of our earlier articles, we saw the different positional parameters in a shell script. We discussed about the positional parameter $*. There is one more positional parameter, $@, the definition of which is also the same as $*. Let us see in this article the exact difference between the parameters $* and $@.

Tuesday, March 1, 2011

sqlplus - Access multiple query results from shell

  In one of our earlier articles, we saw how we can execute a SQL query by connecting to sqlplus. In this, we will see how we can execute multiple queries and access the values inside shell.

Tuesday, February 15, 2011

Cygwin:- What is Cygwin & How to set up?

 "Cygwin is a Unix-like environment and command-line interface for Microsoft Windows" says Wiki. Programmers who wants to practice Unix usually install RedHat Linux in their PC's usually as a dual-boot or through VM. This process demands us to completely boot the OS before we start working. For users who are interested only in programming like C, C++, Java, Shell scripting, Perl scripting, etc.. we dont need to install the entire RedHat linux at all. Cygwin is the perfect alternative for you. Cygwin is a utility like any windows software. Once cygwin is installed, working on Unix is just like working on say a Microsoft word document. Just double click on the shortcut and you are inside the terminal. No need to wait for booting, etc. Cygwin installs every Unix command like a windows executable and hence invokes just like any windows utility.

Tuesday, February 8, 2011

sed - Replace or substitute file contents

 In one our earlier articles, we saw how to insert a line or append a line to an existing file using sed. In this article, we will see how we can do data manipulation or substitution in files using sed.

Let us consider a sample file, sample1.txt, as shown below:

Thursday, February 3, 2011

3 different ways of renaming a group of files

  While dealing with multiple file operations, it is very common to have a scenario where we would like to rename to set of files, say set of txt files to some other extension, or a set of non-extension files to a new 
extension, etc. In this article, we will see 3 different ways to rename the files.

1. To rename a set of stand alone files to a .txt extension. Assuming the directory contains the files file1, file2 and file3.

Wednesday, January 19, 2011

sed - Include or Append a line to a file

 sed is one of the most important editors we use in UNIX. It supports lot of file editing tasks. In this article, we will see a specific set of sed options.

Assume I have a flat file, empFile, containing employee name and employee id as shown below:
Hilesh, 1001
Bharti, 1002
Aparna, 1003
Harshal, 1004
Keyur, 1005

Tuesday, January 11, 2011

Positional parameters in a shell script

 Positional parameters in a shell script are nothing but the command line arguments passed to a shell script. The following are some of the positional parameters used:
$# -  Total number of arguments
$0 - Command or the script name
$1,$2, $3 - First, second and third args respectively.
$* - All the command line arguments starting from $1.