The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Country Codes script faster response ;please help zanetti321 UNIX for Advanced & Expert Users 12 04-24-2008 12:49 PM
IP::Country 2.24 (Default branch) iBot Software Releases - RSS News 0 03-05-2008 02:20 AM
change country in date command zedex AIX 4 12-19-2007 09:35 AM
Query related to for i in `cat $TEMP_DIR/country.txt sureshg_sampat Shell Programming and Scripting 1 08-31-2007 04:58 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-31-2008
Registered User
 

Join Date: Feb 2008
Posts: 38
Script for Country Codes

Dear All

I have a file which contains lines looks like this:

ISC Egypt-Alex2 126 104541338 218926893238 f 1B

ISC BT-Colindale 26 249126190534 218913486850 b 29

ISC Egypt-Cairo2 199 129026052 218927661509 b 26


As you see in each line $4 and $5 are phone numbers , i want a script which process this file and its output should be like this :

ISC Egypt-Alex2 126 104541338 218926893238 f 1B Egypt-Vodafone Libya-Libyana


ISC BT-Colindale 26 249126190534 218913486850 b 29 ZAIN Sudan Libya-Madar

ISC Egypt-Cairo2 199 129026052 218927661509 b 26 Mobinil-Egypt Libya-Libyana


As you see there i need two fields to be added according the country code of the phone numbers

For sure there are another country codes so please can any one advise how could it be done on some country codes as example and i will add the others by analogy

Thanks and waiting
Zanetti321
Reply With Quote
Forum Sponsor
  #2  
Old 03-31-2008
Registered User
 

Join Date: Feb 2008
Posts: 38
Additional Info

Adding to my last request the file containing this line contains about 300000 lines so the script should be somehow fast.

Thanks
Zanetti
Reply With Quote
  #3  
Old 03-31-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Not going to happen until you tell us where we can obtain the information that you want to be added to each line.

If you need it to be fast, it sounds like you might want to port all of this to an SQL database, depending somewhat on how often you will need to do this processing.
Reply With Quote
  #4  
Old 03-31-2008
Registered User
 

Join Date: Feb 2008
Posts: 38
Reply

Dear Era

The Information needed will be a simple file for example list of country codes and the corresponding country for example:

Egypt-Vodafone 2010
Egypt-Mobinil- 2012
Vodafone-UK 447
ZAIN-Sudan 24912
Libya-Libyana 21892

So the script should add the two new fields with the names of countries according to code of $4 and $5

i can prepare to you a file and send it to you if this is the information you need , if else please update me

Regarding the speed i wont prefer to load the data to my SQL because it will waste time for me so please can you have subsitute solution after sending you the required file you need.

Thanks
Zanetti
Reply With Quote
  #5  
Old 03-31-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
You want speed, but loading it up into a faster engine is a waste of your time? Well, your call.

Load the prefix code to operator mapping into a hash and the rest will be trivial. Prepare to learn some Perl if you don't know it already.

Code:
#!/usr/bin/perl

use warnings;
use strict;

my %mapping;

open (F, "mappings.txt") || die "$0: could not open mappings.txt: $!\n";
while (<F>) {
  chomp;
  my ($op, $code) = m/(.*\S)\s+(\d+)$/;
  $mapping{$code} = $op;
}
close F;

my $keys = join ("|", keys %mapping);
my $r = qr/^($keys)/;

while (<>)
{
  my @F = split;
  if (defined $F[3] && $F[3] =~ $r) {
    print "$F[3] matches $1, maps to $mapping{$1}\n";
  }
  if (defined $F[4] && $F[4] =~ $r) {
    print "$F[4] matches $1, maps to $mapping{$1}\n";
  }
}
This is only very briefly tested, proof of concept code. The output is not what you want, but I trust you can glue things together (by trial and error, if not otherwise). Google for a quick Perl intro if you are not familiar with the language, there are tons of those.
Reply With Quote
  #6  
Old 03-31-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
In case it's not painfully obvious, it expects the operator to mapping table in a text file called mappings.txt in the current directory, and reads standard input. Here is a demo run.

Code:
vnix$ ls -l map mappings.txt
-rw-r--r-- 1 era era 516 2008-04-01 09:06 map
-rw-r--r-- 1 era era  93 2008-04-01 09:02 mappings.txt

vnix$ perl ./map <<HERE
> ISC Egypt-Alex2 126 104541338 218926893238 f 1B
> ISC BT-Colindale 26 249126190534 218913486850 b 29
> ISC Egypt-Cairo2 199 129026052 218927661509 b 26
> HERE
218926893238 matches 21892, maps to Libya-Libyana
249126190534 matches 24912, maps to ZAIN-Sudan
218927661509 matches 21892, maps to Libya-Libyana

vnix$ cat mappings.txt
Egypt-Vodafone 2010
Egypt-Mobinil- 2012
Vodafone-UK 447
ZAIN-Sudan 24912
Libya-Libyana 21892
Reply With Quote
  #7  
Old 04-01-2008
Registered User
 

Join Date: Feb 2008
Posts: 38
Reply

Dear Era

Thanks for your reply; just i want to check on a certain issue:

Regarding mapping.txt files it will contain large number of country codes ; so will this affect or it is ok?

Regarding your code what i understand that the first block is loading the mapping.txt into hash then the second block is reading my lines and matching it with the hash contents

If you please tell me the meaning of each line just to understand if this will disturb you so no problem.

Thanks for co-operation
Zanetti
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0