Korn shell - lookup table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn shell - lookup table
# 1  
Old 04-26-2016
Korn shell - lookup table

Hi All

I need to pass country code into a pipe delimited file for lookup.
It will search country code (column 3) in the file, if the country code matched, it will return value from other columns.

Here is my mapping file.
Code:
#CountryName|CountryRegion|CountryCode-3|CountryCode-2
Aruba|AMS|ABW|AW
Anguilla|AMS|AIA|AI
Netherlands Antilles|AMS|ANT|AN

For instance:
If I pass in AIA, i should get country region as AMS and country name as Anguilla.

And here is my code so far:
Code:
while IFS='|' read -r f1 f2 f3 f4
do
  if [ $f3 = $country_code ]; then
    CountryName=$f1
    CountryRegion=$f2
    CountryCode2=$f4
  fi
done < "$input"

Could you please advise whether there is a better way for this requirement without using looping?

Thank You
# 2  
Old 04-26-2016
Quote:
Originally Posted by lafrance
Could you please advise whether there is a better way for this requirement without using looping?
That is good enough. The loop itself won't cost you any time, at least not compared to opening and reading the file (which is, for a short list, also neglectable).

You might want to think to put the whole mechanism into a function, like this (just a sketch):

Code:
#! /bin/ksh

function pGetCountryDetails
{
typeset f1=""
typeset f2=""
typeset f3=""
typeset f4=""
typeset country_code="$1"

while IFS='|' read -r f1 f2 f3 f4 ; do
     if [ $f3 = $country_code ] ; then
          CountryName=$f1
          CountryRegion=$f2
          CountryCode2=$f4
          return 0
     fi
done < "$input"

return 1
}


# main()

[... get some country_code....]

if ! pGetCountryDetails "$country_code" ; then
     print -u2 - "country code $country_code not found in file"
else
     print - "Country Code is: $country_code"
     print - "CountryName=$CountryName"
     print - "CountryRegion=$CountryRegion"
     print - "CountryCode2=$CountryCode2"
fi
[.... etc. ....]

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 04-26-2016
What about the following;

Code:
grep -n AIA input.txt | cut -d ':' -f1 | xargs -I % awk -F '|' 'NR==% {print "country region is "$2 " country name is " $1}'  input.txt

# 4  
Old 04-26-2016
Code:
while IFS=| read f1 f2 f3 f4
do
  mkdir $f3
  echo $f1 >$f3/name
  echo $f4 >$f3/short
  echo $f2 >$f3/region
done

Code:
ISO=CAN
countryname=`cat $ISO/name`

# 5  
Old 04-26-2016
Instead of reading your file every single time you need to access the data, you could populate an associative array with your data
Code:
typeset -A CNAME CREG CCOD2
{ read
  while IFS='|' read -r f1 f2 f3 f4
    do  CNAME[$f3]=$f1
        CREG[$f3]=$f2
        CCOD2[$f3]=$f4
    done
} < "$input"

and then use those in a function as proposed before, or immediately whenever needed, like
Code:
for i in ${!CNAME[@]}; do echo $i: ${CNAME["$i"]}, ${CREG["$i"]}, ${CCOD2["$i"]}; done
ABW: Aruba, AMS, AW
AIA: Anguilla, AMS, AI
ANT: Netherlands Antilles, AMS, AN

(I can't test this in ksh but I'm quite confident it will work, eventually with minor adaptions)
# 6  
Old 04-26-2016
The associative array method makes sense if you have multiple lookups in the script.
It works with bash-4 and ksh93.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Script: User Lookup

Hi everyone, Let me start by stating this question is for homework help (not "help, my boss needs this ASAP") I have spent the last few days re-visiting this script, and cannot figure out where I am going wrong (something simple I'm sure). I am to build a script that searches for a user... (1 Reply)
Discussion started by: jjc032681
1 Replies

2. Shell Programming and Scripting

Filtering duplicates based on lookup table and rules

please help solving the following. I have access to redhat linux cluster having 32gigs of ram. I have duplicate ids for variable names, in the file 1,2 are duplicates;3,4 and 5 are duplicates;6 and 7 are duplicates. My objective is to use only the first occurrence of these duplicates. Lookup... (4 Replies)
Discussion started by: ritakadm
4 Replies

3. UNIX for Dummies Questions & Answers

string replacement using a lookup table

Dear all thanks for helping in advance.. Know this should be fairly simple but I failed in searching for an answer. I have a file (replacement table) containing two columns, e.g.: ACICJ ACIDIPHILIUM ACIF2 ACIDITHIOBACILLUS ACIF5 ACIDITHIOBACILLUS ACIC5 ACIDOBACTERIUM ACIC1 ACIDOTHERMUS... (10 Replies)
Discussion started by: roussine
10 Replies

4. Shell Programming and Scripting

Sed variable from lookup table

I have a file with the following format --TABLEA_START-- field1=data1;field2=data2;field3=data3 --TABLEA_END-- --TABLEB_START-- field1=data1;field2=data2;field3=data3 --TABLEB_END-- --TABLEA_START-- field1=data1;field2=data2;field3=data3 ... (0 Replies)
Discussion started by: milo7
0 Replies

5. Programming

64-bit CRC Transition To Bytewise Lookup-Table

Good Evening, I started working on the 17x17 4-colouring challenge, and I ran into a bit of an I/O snag. It was an enormous headache to detect the differences in very similar 289-char strings. Eventually, it made more sense to associate a CRC-Digest with each colouring. After learning... (0 Replies)
Discussion started by: HeavyJ
0 Replies

6. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

7. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

8. UNIX for Dummies Questions & Answers

HELP with using a lookup table

Using AIX 5.2, Bourne and Korn Shell. I have two flat text files. One is a main file and one is a lookup table that contains a number of letter codes and membership numbers as follows: 316707965EGM01 315672908ANM92 Whenever one of these records from the lookup appears in the main file... (6 Replies)
Discussion started by: Dolph
6 Replies

9. Shell Programming and Scripting

lookup table in perl??

hi, i am very much new in perl and have this very basic question in the same:( the requirement is as below: i have an input file (txt file) in which i have fields invoice number and customer number. Now i have to take input this combination of invoice n customer number and check in a... (2 Replies)
Discussion started by: Bhups
2 Replies

10. UNIX for Dummies Questions & Answers

Lookup between 2 files ( korn shell )

Hi All., i have a problem. I hope i can get some help on this issue here; i have 2 txt files say file1 and file 2 file1 has; WLMT:XXXXXXXX:cp DOLR:YYYYYYY:ascii,unblock WLG:TTTTTTT:dd:73:ascii,unblock MAR:SSSSSS:dd:152:ascii,unblock GGG:QQQQQQQQQQ:112:ascii,unblock EIE:CCCCCCCC:cp... (17 Replies)
Discussion started by: pavan_test
17 Replies
Login or Register to Ask a Question