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 !!!!!
nawk -f raina.awk textFile
raina.awk:
{
arr[$1] = ($1 in arr) ? arr[$1] "," $2 : $2
}
END {
for (i in arr)
print i, arr
}
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 !
THANKS A TON !
PLEASE IGNORE MY PREVIOUS REPLY..I HAD REPLACED $1 BY $i..SO WAS NOT WORKING
ITS WORKING NOW.
THANKS !
Here's some python for ya:
#!/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])