Thursday, June 28, 2012

awk - 10 examples to split a file into multiple files

 In this article of the awk series, we will see the different scenarios in which we need to split a file into multiple files using awk. The files can be split into multiple files either based on a condition, or based on a pattern or because the file is big and hence needs to split into smaller files.

Sample File1:
Let us consider a sample file with the following contents:
$ cat file1
Item1,200
Item2,500
Item3,900
Item2,800
Item1,600

Tuesday, June 26, 2012

sed - 25 examples to delete a line or pattern in a file

In this article of  sed tutorial series, we are going to see how to delete or remove a particular line or a particular pattern from a file using the sed command.

 Let us consider a file with the sample contents as below:
$ cat file
Cygwin
Unix
Linux
Solaris
AIX
1. Delete the 1st line or the header line:
$ sed '1d' file
Unix
Linux
Solaris
AIX
      d command is to delete a line. 1d means to delete the first line.

Monday, June 25, 2012

A forum of The UNIX School

Dear Unix School Subscribers,
   Something which I have been thinking for quite some time, to have a dedicated forum for our blog is now ready, The UNIX School Forum.This forum is a place where-in you can get all your doubts clarified on Shell Scripting, sed, awk, perl, etc. All the users who till now have been sending mails to clarify their queries can simply post a question here and get the answer. So, what are you waiting for? Simply go the forum tab and post a question. 

Wednesday, June 20, 2012

Insert a line before or after a pattern

  In this article, we will see the different ways in which we can insert a line before or after on finding a pattern.  On finding the pattern 'Fedora', the requirement is to insert a line:
  • before the pattern 
  • after the pattern.
Let us consider a file with the following contents:
$ cat file
Linux
Solaris
Fedora
Ubuntu
AIX
HPUX
Inserting before the pattern:

Monday, June 18, 2012

Shell script to send birthday reminder e-mails

   In a professional work place, it is pretty common to celebrate the birthdays of your colleagues. Somebody in the team will have the birthdays maintained in a file and they will let the team know when the day arrives. The issue here is it is quite possible to miss out if one forgets to check the birthday file. How nice it will be if you can get an e-mail reminder of the person celebrating their birthday? Let us see in this article how to create a shell script which will send us an e-mail stating the names of the people celebrating their birthdays. Also, the same script with little modifications can be used to set reminder for any other purpose.

  This article discusses about 3 scripts:
  • Shell Script to get reminder on the list of users having their birthday today.
  • Shell  Script to get reminder on the list of users having their birthday the next day or some other day.
  • Shell  Script to get reminder on the list of all users having their birthday over the next n days.

Wednesday, June 13, 2012

Swap every 2 lines in a file

In an earlier article, we had discussed the different ways in which we can join every two lines in a file. In this article, we will see the different ways in which we can swap every two lines in a file.

Let us consider a file with the following contents:
$ cat file
Linux
Solaris
AIX
Ubuntu
Fedora

Monday, June 11, 2012

10 tips to improve Performance of Shell Scripts

   If writing good Shell scripts is a skill, writing better performance shell scripts is an art.  Shell scripts are any Unix admin's asset. It is a major time saver for repetitive activities in Unix flavors. When a shell script is written, by default, the big target in-front of the developer is to make the script running, and of course, to give the right result. As long as these scripts are intended for some user account activities which does not involve huge data, it does not matter how the result is arrived at. However, the same cannot be said on those Shell scripts which are meant to be executed on production servers containing millions of customer records present in huge files.
 
     A Shell script can give the right result, but if the result does not arrive in a considerable time, it is an issue, and yes, it can become a nightmare for the support staff. Every second is important in a production environment. A script which can be improved by even some 10 seconds can be considered an improvement depending on the environment. In this article, we will discuss about some import things which a shell script developer should keep in mind while writing shell scripts to give better performance.

Wednesday, June 6, 2012

5 ways to reverse the order of file content

 In this article, we will see the different methods in which we can print the file content in reverse order.

Let us consider a file with the following contents:
$ cat file
Linux
Solaris
AIX
1. tac command is the reverse of cat. It simply prints the file in reverse order.
$ tac file
AIX
Solaris
Linux
   Note: tac is not available in all Unix flavors.

Monday, June 4, 2012

awk - 10 examples to group data in a CSV or text file

awk is very powerful when it comes for file formatting.  In this article, we will discuss some wonderful grouping features of awk. awk can group a data based on a column or field , or on a set of columns. It uses the powerful associative array for grouping. If you are new to awk, this article will be easier to understand if you can go over the article how to parse a simple CSV file using awk.

Let us take a sample CSV file with the below contents. The file is kind of an expense report containing items and their prices. As seen, some expense items  have multiple entries.
$ cat file
Item1,200
Item2,500
Item3,900
Item2,800
Item1,600