The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Combining files horizontally anshuljain HP-UX 3 03-14-2008 05:51 AM
Combining two files hemangjani Shell Programming and Scripting 7 06-13-2007 10:32 PM
Combining Two Files bat711 Shell Programming and Scripting 3 10-05-2005 01:26 PM
Combining files Enda Martin UNIX for Dummies Questions & Answers 2 07-20-2001 10:31 AM
combining files apalex UNIX for Dummies Questions & Answers 3 06-19-2001 09:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-20-2006
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
Question Combining Two Files

Could someone help me reduce the number of runs for a shell program I created?

I have two text files below:

Code:
$ more list1.txt
01 AAA
02 BBB
03 CCC
04 DDD

$ more list2.txt
01 EEE
02 FFF
03 GGG

I want to combine the lines with the same number to get the below:

01 AAA 01 EEE
02 BBB 02 FFF
03 CCC 03 GGG

I made a shell which does this. The number of runs for this shell is the product of the number of lines in input1 and input2. I have a very large input and when I used this utility it took a long time to process and was wondering if there's another method to do this with less number of runs.

Code:
$ more combine.ksh
#!/bin/ksh

while read number1 text1
do
  while read number2 text2
    do
       [[ $number1 = $number2 ]] && echo "$number1 $text1 $number2 $text2"
    done < $2
done < $1

Last edited by stevefox; 02-20-2006 at 03:30 AM..
  #2 (permalink)  
Old 02-20-2006
Manish Jha Manish Jha is offline
Registered User
  
 

Join Date: Dec 2005
Location: Boston, USA
Posts: 65
you can use simply paste file1 file2. This works fast also and hope it will resolve your problem.

Regards,
Manish Jha.
  #3 (permalink)  
Old 02-20-2006
gauravgoel gauravgoel is offline
Registered User
  
 

Join Date: Dec 2005
Location: India
Posts: 218
see man pages for "paste" I guess it should work
  #4 (permalink)  
Old 02-20-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Here is a certain improvement to your script.

Code:
[[ $number1 = $number2 ]] && echo "$number1 $text1 $number2 $text2" && break;
It will skip those succeeding elements after a match is found.

You might want to think of putting the elements of list2 in an array and then remove them dynamically (if it can be done..). All of these put together, I think you are better off with paste.

Last edited by vino; 02-20-2006 at 04:06 AM..
  #5 (permalink)  
Old 02-20-2006
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
Smile

Thanks guys!!

I was able to reduce it to one line below:

Code:
paste $1 $2 | grep ".* .* .*"
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 04:06 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