Tuesday, October 20, 2009

Filter rows in a file based on composite key in another file

Here is a quick, simple and easy way to filter the records in a file based on a composite key present in another file!


There are many ways to do it, this method uses ask, grep & sort to do the job

Assumptions:
  1. Files are pipe ("|") separated
  2. File containing the key is called "accounts"
  3. Columns 2 & 4 join to form the key to be used for filtering
  4. File containing the records to be filtered is called "acdata"
  5. This file contains the "key" column apart from other data
  6. Filtered records need to be stored in the file "filtdata"

awk 'BEGIN { FS="|";} {k1=$2; k2=$4; k=k1 k2; print "grep " k  " acdata" ;}' accounts | sort -u | bash > filtdata


Sort with unique option turned on needs to be used to ensure that we get only one row from the key file in case there are more than one rows with the same composite key.

No comments:

LinkWithin

Related Posts Plugin for WordPress, Blogger...