Country Codes script faster response ;please help


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Country Codes script faster response ;please help
# 1  
Old 04-23-2008
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 Satellite 88216
Lebanon 964
England 44

and so on this file will be containning country codes of alot of countries

I have a script which work on these files and the output look like this

Belgium1 ISC 3 924556808 393475157928 b 1B Libyana Italy
Italy ISC 151 925082838 447717254923 b 26 Libyana England
ISC BT-GlasGow 152 88216900500 218925288836 f 1B Thuraya Libyana
ISC Spain-Telefonica 14 964188208970 218925735325 b 29 Lebanon Libyana

as shown there are two added fields according to the country codes

The problem that the script i am using is slow because i have large input lines (may reach 300000) and i have lot of countries so i want a faster code

The code i have is:
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}'

So do you have faster one to be more reliable ?

Note : I am using Solaris 8 SUN280R

Please advise
Zanetti
# 2  
Old 04-23-2008
Sorry

Sorry but i forget to put the input lines which is mentioned in my first words; it look like this:

Belgium1 ISC 3 924556808 393475157928 b 1B
Italy ISC 151 925082838 447717254923 b 26
ISC BT-GlasGow 152 88216900500 218925288836 f 1B
ISC Spain-Telefonica 14 964188208970 218925735325 b 29

Thanks
Zanetti
# 3  
Old 04-23-2008
The perl script I wrote three weeks ago might be faster than what you ended up with; have you tested?

https://www.unix.com/unix-advanced-ex...#post302180718

Do you have some sort of timing data, what's the current performance and what would you like for it to be?
# 4  
Old 04-23-2008
Ugh, looking closer at that code, I can see that it's going to be slow. It starts a separate awk for each telephone code! Please test the Perl script on some real-world data; if it feels fast enough, we can work on making it format the output the way you want it.

(Certainly the awk code would be possible to optimize, at least to make it a bit less hungry for processes; but if your awk is old and tired, Perl is probably easier to work with. At least it won't throw syntax errors if we don't have exactly the same version.)

Last edited by era; 04-23-2008 at 02:11 PM..
# 5  
Old 04-23-2008
Actually, this any better?

Code:
vnix$ sed -e 's%\(.*\) \(.*\)%$4 ~ /^\2/ { c1=" \1" } $5 ~ /^\2/ { c2=" \1" }%' \
>   -e '$a\{ print $0 c1 c2; c1 = c2 = ""; }' country-codes.txt |
> awk -f - phonelines.txt
Belgium1 ISC 3 924556808 393475157928 b 1B Italy
Italy ISC 151 925082838 447717254923 b 26 England
ISC BT-GlasGow 152 88216900500 218925288836 f 1B Thuraya Satellite Libyana
ISC Spain-Telefonica 14 964188208970 218925735325 b 29 Lebanon Libyana

The output is ambiguous when you have one code but not the other, is that a problem?

Last edited by era; 04-23-2008 at 02:16 PM.. Reason: Simplify sed script further; reset c1 = c2 = "" at end
# 6  
Old 04-23-2008
Reply

Dear Era

The problem is that the perl script dont output my need at all ; i prefer to use the last code which is :

vnix$ sed -e 's%\(.*\) \(.*\)%$4 ~ /^\2/ { c1=" \1" } $5 ~ /^\2/ { c2=" \1" }%' \
> -e '$a\{ print $0 c1 c2; c1 = c2 = ""; }' country-codes.txt |
> awk -f - phonelines.txt

Do you mean that these lines will be wrote in a seperate then change to excutable and run it or run it manually from the command line

Is this seems faster; also does it in need any modifications?

Zanetti
# 7  
Old 04-24-2008
The Perl code is a proof of concept. If it's faster (my uninformed guess is it will be) then changing it so it prints what you want is trivial, and you probably don't even need to ask here how to change it (but feel free if you can't figure it out).

Do what you will with the code; if having it in a file is more convenient then by all means put it in a file and mark it executable. Whatever you did with the previous script, you can do with this one. This is just an optimized version of unilover's script.

If it prints what you want, then it doesn't require modification. You tell us.

Can you run this one and the Perl script on your real-world data and report how long they take to run? (Never mind that the Perl script doesn't print the stuff you want; changing that won't make it faster or slower.) If the Perl script is not a lot faster then you can probably live with this awk+sed combo.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script works, but I think it could be better and faster

Hi All, I'm new to the forum and to bash scripting. I did some stuff with VB.net, Batch, and VBScripting in the past, but because I shifted over to Linux, I am learning to script in Bash at this moment. So bear with me if I seem to script like a newbie, that's just because I am ;-) OK, I... (9 Replies)
Discussion started by: cornelvis
9 Replies

2. Shell Programming and Scripting

Optimize shell script to run faster

data.file: contact { contact_name=royce-rolls modified_attributes=0 modified_host_attributes=0 modified_service_attributes=0 host_notification_period=24x7 service_notification_period=24x7 last_host_notification=0 last_service_notification=0 host_notifications_enabled=1... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

Making script run faster

Can someone help me edit the below script to make it run faster? Shell: bash OS: Linux Red Hat The point of the script is to grab entire chunks of information that concerns the service "MEMORY_CHECK". For each chunk, the beginning starts with "service {", and ends with "}". I should... (15 Replies)
Discussion started by: SkySmart
15 Replies

4. Shell Programming and Scripting

Make script faster

Hi all, In bash scripting, I use to read files: cat $file | while read line; do ... doneHowever, it's a very slow way to read file line by line. E.g. In a file that has 3 columns, and less than 400 rows, like this: I run next script: cat $line | while read line; do ## Reads each... (10 Replies)
Discussion started by: AlbertGM
10 Replies

5. Shell Programming and Scripting

Script to parse a file faster

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2 op=-1 msgId=-1 -... (7 Replies)
Discussion started by: sags007_99
7 Replies

6. Shell Programming and Scripting

Can anyone make this script run faster?

One of our servers runs Solaris 8 and does not have "ls -lh" as a valid command. I wrote the following script to make the ls output easier to read and emulate "ls -lh" functionality. The script works, but it is slow when executed on a directory that contains a large number of files. Can anyone make... (10 Replies)
Discussion started by: shew01
10 Replies

7. UNIX for Advanced & Expert Users

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... (19 Replies)
Discussion started by: zanetti321
19 Replies

8. Shell Programming and Scripting

How the first script should notify in case there is no response from second

Hi Experts, I am trying to write a ksh script that it should notify in case there is no response from the other script. I mean to say that I got a.sh and b.sh the execution of b.sh depends on a.sh, so if there is no response from a.sh, b.sh should notify me about the same. Thanks in Advance (4 Replies)
Discussion started by: rajusa10
4 Replies
Login or Register to Ask a Question