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. |
|||
Technorati Tags: 
Recent comments
5 days 15 hours ago
2 weeks 16 hours ago
4 weeks 2 days ago
4 weeks 5 days ago
5 weeks 5 days ago
6 weeks 6 days ago
7 weeks 5 days ago
8 weeks 3 days ago
9 weeks 14 hours ago
10 weeks 2 days ago