Script for Country Codes


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Script for Country Codes
# 8  
Old 04-01-2008
If you like, you can also do it with the following two lines:

Code:
sed 's=\(.*\) \(.*\)=awk :{if($4~/^\2/)print $0" \1";else print}: phonelines.txt=' country-codes.txt|tr ':' "'"|sh|awk 'NF==8{print}' >newlines.txt
sed 's=\(.*\) \(.*\)=awk :{if($5~/^\2/)print $0" \1";else print}: newlines.txt=' country-codes.txt|tr ':' "'"|sh|awk 'NF==9{print}'

in which:

phonelines.txt is the file containing your data-lines and
country-codes.txt is the file containing the two-column phone-codes.
# 9  
Old 04-01-2008
Or, for a faster code, execute:

Code:
awk "{$(sed 's=\(.*\) \(.*\)=if($4~/^\2/)print $0\" \1\"=' country-codes.txt)}" phonelines.txt|awk "{$(sed 's=\(.*\) \(.*\)=if($5~/^\2/)print $0\" \1\"=' country-codes.txt)}"

with country-codes.txt containing:

Code:
Egypt-Vodafone 2010
Egypt-Mobinil 2012
Vodafone-UK 447
ZAIN-Sudan 24912
Libya-Madar 21891
Libya-Libyana 21892
Country10-City4 104
Country12-City9 129

and phonelines.txt having your 3 sample lines, the output will be:
Code:
ISC Egypt-Alex2 126 104541338 218926893238 f 1B Country10-City4 Libya-Libyana
ISC BT-Colindale 26 249126190534 218913486850 b 29 ZAIN-Sudan Libya-Madar
ISC Egypt-Cairo2 199 129026052 218927661509 b 26 Country12-City9 Libya-Libyana

# 10  
Old 04-02-2008
Dear Unilover

I would like to thank you very much for your code , i think it is the one that i want ,

Please can you tell me about the sed scripting you used , can you tell for example what /n.* make and so one for the code you created

Regarding speed can your code support up to for example 500,000 lines in short time or it will take much time?

Thnaks very much again
Zanetti
# 11  
Old 04-02-2008
OK, this is what you have to do in order to understand how this code works:

1. Execute:
Code:
sed 's=\(.*\) \(.*\)=if($4~/^\2/)print $0\" \1\"=' country-codes.txt

It will convert each line of the "country-code" to an "if-statements" in awk-language that instructs awk that:

if the forth-field (of the read-in line) starts with the "number" then, print the entire line followed by the "name".

Thus, if you run awk on the phone-lines.txt file with those generated "if-statements" as the executing-commands on the read-in lines, the result will be "addition of the appropriate Country-City-Name to the end of the line, based on the starting number in the forth-field of the read-in line".

You can see it for yourself, by executing:
Code:
awk "{$(sed 's=\(.*\) \(.*\)=if($4~/^\2/)print $0\" \1\"=' country-codes.txt)}" phonelines.txt

Now, we have to add the "Second Country-City-Name to the end of the line, based on the starting number in the fifth-field of the read-in line".

We do this by piping the output of the above-command to another awk who has no phonelines.txt as its file-parameter (so, it will be forced to read the output of the previous awk in the pipe) and, as its set of "if-statements", the same sed is used but this time it is generating "$5~/^ ..." instead of "$4~/^ ..." to instruct (the second) awk that:

if the fifth-field (of the read-in line) starts with the "number" then, print the entire line followed by the "name".

Now, as for the speed, I'm afraid I don't have a 500000 line phonelines.txt file and more than that, it depends on the computer on which you're running this commands. But, one thing that I am very sure of is that both sed and awk are very fast in what they do.

As for the description of how the sed is generating the "if-statements", I would refer you to the description of "Regular Expression" either on the internet onin man-pages.

Good LuckSmilie
# 12  
Old 04-03-2008
Variable syntax

Dear Unilover

Thanks for your code but i typed it the script you gave me the result is
Variable Syntax

So i think there may problem in Syntax

Please advise

Zanetti
# 13  
Old 04-03-2008
I believe you should have made a mistake in typing. Please copy what you have typed and paste it here so that I can see where the mistake is.
# 14  
Old 04-03-2008
Reply

Dear Unilover
this what i have typed for the faster code:

geo@spiserver /home/geo/cdr/ItalyRel 25 > awk "{$(sed 's=\(.*\) \(.*\)=if($4~/^\2/)print $0\" \1\"=' country-codes.txt)}" phonelines.txt|awk "{$(sed 's=\(.*\) \(.*\)=if($5~/^\2/)print $0\" \1\"=' country-codes.txt)}"
Variable syntax

and this is the files in my directory

134 Apr 3 16:56 country-codes.txt
-rw-r--r-- 1 geo geo 0 Apr 3 16:57 final
-rw-r--r-- 1 geo geo 874008 Mar 31 17:50 italy2
-rw-r--r-- 1 geo geo 1151226 Apr 3 17:11 phonelines.txt
-rwxr-xr-x 1 geo geo 133 Apr 3 17:09 script*
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Forum Support Area for Unregistered Users & Account Problems

Registering from blocked country

Hi, I'm come from Vietnam and want to join to Unix forum just because I like Unix programming and want to learning more. My IP is allocate by DHCP server so it is dynamic. Here is my desired username and my email: Username: lucasdo Email: rennersstar@gmail.com Thank you very much for... (0 Replies)
Discussion started by: lucasdo
0 Replies

2. Shell Programming and Scripting

ip 2 country sorting[solved]

Dear folks. I have list of ip address from different country, i would like check from script this ip address belong to which country, please suggest any command, i have more than 200 ip address so i need to do it via script. Script will show me country code and its City if possible? ... (0 Replies)
Discussion started by: learnbash
0 Replies

3. Shell Programming and Scripting

Bash Shell Script Exit Codes

Here is my daily stupid question: How can I tell a script to only execute if the other scripts exits successfully? So "script A" executes and it executes successfully (0),then "script B" will run or else "script A "executes and it exits unsucessfully (1) then "script B" will read return... (6 Replies)
Discussion started by: metallica1973
6 Replies

4. Shell Programming and Scripting

'watch' not interpreting escape codes in bash script

Hi there, I'm fairly new to bash scripting and already having some troubles. I'm making a script that can print some series of strings in colors based in the information of a file, for simplicity let's say it only does: #!/bin/bash printf "\eWhen you execute this in the command line it... (1 Reply)
Discussion started by: Arashi
1 Replies

5. Shell Programming and Scripting

whois country help

Hello folks, I have list of ips like 1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 whois 1.1.1.1 |grep -E 'country|Country' it show country=US or whatever. so i have number of ips in text file, how i can use above script to automate output like 1.1.1.1 US 2.2.2.2 CA 3.3.3.3 FR (3 Replies)
Discussion started by: learnbash
3 Replies

6. UNIX for Advanced & Expert Users

Country Codes script faster response ;please help

Dear all I have group of input lines which look like this These input lines is placed in a file named phonelines.txt and there is a script which match $4 and $5 with country codes placed in another file named country-codes.txt and its contents is : Italy 39 Libyana 21892 Thuraya... (12 Replies)
Discussion started by: zanetti321
12 Replies
Login or Register to Ask a Question