Joining flat files using shell scripting

Well, I like databases and thought why don't make a small relational database based on flat files and do joining them using Linux/Unix shell. I tried to invent this small example:

1. Have 2 flat text files, one for Employees (emp.tbl) and the other for Departments (dept.tbl), like the following:

emp.tbl content:
emp_id emp_name dept_id
1      Rami     1
2      Emad     1
3      Mary     2
4      Hassan   1
5      Rasha    2

dept.tbl content:
dept_id dept_name 
1       I.T.
2       H.R.

2. Try this small shell script (join.sh):
#!/bin/bash
# Description: Joining 2 flat files (emp.tbl & dept.tble)
# Version: 0.1
# License: Creative Commons 2.5
# Created at: 2006/07/30 By Rami Sedhom ikhnaton2[at]gmail[dot]com

# i begins from 2 to ignore header row
i=2 

# Loop for emp.tbl lines
while [ $i -le `cat emp.tbl | wc -l` ]
do
  # Print each line to temp file and assign 3rd column (foreign key) to variable
  f=`sed -n $i\p emp.tbl | tee row-emp.tmp | awk '{print $3}'` 
  # Search dept.tbl for using key value in the variable $f
  grep $f dept.tbl > row-dept.tmp
  # Print result rows side by side
  paste row-emp.tmp row-dept.tmp
  # increment $i
  i=`expr $i + 1`
done
# Remove temp files
rm -rf row-emp.tmp row-dept.tmp

The results showing each employee with his associated department:
1 Rami 1        1 I.T.
2 Emad 1        1 I.T.
3 Mary 2        2 H.R.
4 Hassan 1      1 I.T.
5 Rasha 2       2 H.R.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <ins> <del> <blockquote> <span> <center>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You may write mixed Arabic and English freely, line direction will be computed automaticaly

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.
Syndicate content