![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 !!!!! |
|
||||
|
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 ! |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|