The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To call/execute a shell script from a shell script konark UNIX for Dummies Questions & Answers 1 10-26-2007 06:16 PM
how to execute shell commands in awk script? RishiPahuja Shell Programming and Scripting 1 09-02-2005 10:37 AM
Execute Shell Script sam71 UNIX for Dummies Questions & Answers 1 04-29-2005 02:43 PM
Is it possible to execute shell script with PHP? Micz Shell Programming and Scripting 1 05-27-2004 12:52 PM
How to execute shell Script? siva Shell Programming and Scripting 3 05-14-2002 12:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-16-2007
nvuradi nvuradi is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 23
Shell script is taking more than 3 hrs to execute

Hi



I am doing a process of converting all the values of key column into a row, for eg



Key col1 col2

1 1 1

1 2 1

1 1 3

1 3 1

2 1 1

2 1 2



What I am doing in the Script is convert this data into



1(key)|1:2:1:3 (All Col1 values),1:1:3:1(all col2 values)

2(key)|1:1,1:2

TO achieve this i am using two while loops and 4 If else loops

Now in my production the number of columns are 4 and the number records in the input file are 0.2 million (2 lac) and this script is taking more than 3 hrs to run.

Any idea on how to minimize the execution time?
  #2 (permalink)  
Old 11-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
No error checking ! Not complete !

Using hash, this should be super-fast !

Code:
#! /opt/third-party/bin/perl

open(FILE, "<", "r");

while(<FILE>) {
  next if(/^$/);
  chomp;
  my @arr = split(/ /);
  my @val = split(/,/, $fileHash{$arr[0]});
  $val[0] .= (":" . $arr[1]);
  $val[1] .= (":" . $arr[2]);
  $val[0] .= ("," . $val[1]);
  $val[0] =~ s/,:/,/;
  $val[0] =~ s/^://;
  $fileHash{$arr[0]} = $val[0];
}

close(FILE);

foreach my $k ( keys %fileHash ) {
  print "$k $fileHash{$k}\n";
}

exit 0
  #3 (permalink)  
Old 11-18-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
awk

Hi,

This one should be ok.

input:
Code:
1 1 1

1 2 1

1 1 3

1 3 1

2 1 1

2 1 2

3 1 1

4 2 1

4 1 3

1 3 1

2 1 1

2 1 2
optput:
Code:
2|1:1:1:1|1:1:1:1
3|1|1
4|2:1|2:1
1|1:2:1:3:3|1:2:1:3:3
code:
Code:
awk '
{
if (NF>1)
{
	col[$1]=$1
	if (col1[$1]=="")
		col1[$1]=$2
	else
		col1[$1]=sprintf("%s:%s",col1[$1],$2)
	if (col2[$1]=="")
		col2[$1]=$2
	else
		col2[$1]=sprintf("%s:%s",col2[$1],$2)
}
}
END{

for (i in col)
	print i"|"col1[i]"|"col2[i]
}' filename
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0