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
shell script to remove old files and write to a log file yabai Shell Programming and Scripting 4 12-09-2008 01:08 PM
How can i prepare a file by comparing two other files? manmohanpv Shell Programming and Scripting 3 02-18-2008 04:58 AM
comparing PID values of 2 Files in shell Options marconi Shell Programming and Scripting 2 12-19-2007 01:02 PM
Script error.. for comparing 2 files! gkrishnag UNIX for Advanced & Expert Users 4 09-13-2006 09:19 AM
comparing files to contents of a file SummitElse Shell Programming and Scripting 3 06-28-2006 12:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-20-2005
raina_nalin raina_nalin is offline
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 !!!!!
  #2 (permalink)  
Old 06-20-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
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]
}
  #3 (permalink)  
Old 06-21-2005
raina_nalin raina_nalin is offline
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 !
  #4 (permalink)  
Old 06-21-2005
raina_nalin raina_nalin is offline
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 !
  #5 (permalink)  
Old 06-21-2005
pixelbeat pixelbeat is offline
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])
Sponsored Links
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:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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