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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to remove old files and write to a log file yabai Shell Programming and Scripting 3 05-21-2008 12:52 AM
How can i prepare a file by comparing two other files? manmohanpv Shell Programming and Scripting 3 02-18-2008 12:58 AM
comparing PID values of 2 Files in shell Options marconi Shell Programming and Scripting 2 12-19-2007 09:02 AM
Script error.. for comparing 2 files! gkrishnag UNIX for Advanced & Expert Users 4 09-13-2006 06:19 AM
comparing files to contents of a file SummitElse Shell Programming and Scripting 3 06-28-2006 09:36 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-20-2005
Registered User
 

Join Date: Jun 2005
Posts: 3
shell script comparing files in a file

There is a text file that contains the data in the following format:

COLUMN1 COLUMN2
ABC 1
ABC 2
ABC 3
DEF 4
DEF 5
XYZ 7


We have to create a second text file by reading the above file in the following format:

COLUMN1 COLUMN2
ABC 1,2,3
DEF 4,5
XYZ 7


I am new to unix scripting!Kindly help !!!!!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-20-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,991
nawk -f raina.awk textFile

raina.awk:
Code:
{
  arr[$1] = ($1 in arr) ? arr[$1] "," $2 : $2
}
END {
  for (i in arr)
    print i, arr[i]
}
Reply With Quote
  #3 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Jun 2005
Posts: 3
Hello,
Thanks for the effort !
However,I am getting the following output :

ABC 1 1
ABC 2 2
ABC 3 3
XYZ 7 7
DEF 4 4
DEF 5 5

when the input file contained the data as mentioned in the problem.


Also,I could not use "nawk"--It said that --- command not found!

Thanks !
Reply With Quote
  #4 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Jun 2005
Posts: 3
THANKS A TON !
PLEASE IGNORE MY PREVIOUS REPLY..I HAD REPLACED $1 BY $i..SO WAS NOT WORKING :-)
ITS WORKING NOW.

THANKS !
Reply With Quote
  #5 (permalink)  
Old 06-21-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
Here's some python for ya:

Code:
#!/usr/bin/env python
                                                                                                          
import sys
                                                                                                          
groups={}
                                                                                                          
while 1:
     line = sys.stdin.readline()
     if line == '':
         break
     try:
         key,value=line.strip().split()
         if not groups.has_key(key):
            groups[key]=[value]
         else:
            groups[key].append(value)
     except:
         pass
                                                                                                          
keys=groups.keys()
keys.sort()
for key in keys:
    print key,
    print ",".join(groups[key])
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:52 AM.


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

Content Relevant URLs by vBSEO 3.2.0