![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | 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 !!!!! |
| Forum Sponsor | ||
|
|
|
|||
|
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])
|
|||
| Google UNIX.COM |