I need to sort a file with data such as so it breaks on column 1 and all the data in column 2 is sorted into rows with a unique column 1:
1 5
1 6
1 7
2 3
2 4
3 7
3 0
3 9
So it comes out as:
1 5 6 7
2 3 4
3 7 0 9
I've tried many iterations of nawk but can't get it working!!!
Did you try running this code?
This code will give what the output he has given.
if he wants second column to be sorted
sort -kn1 -kn2 file | awk ' ...'
store = {}
all = open("datafile.txt").readlines()
for items in all:
key,value = items.split()
if store.has_key(key):
store[key].append(value)
else:
store[key] = [value]
for i in sorted(store.keys()):
print i, ' '.join(store)
now its working fine ........thanks ghostdog74 and matrixmadhan for pointing out the mistake :o
abt the memory usage....ya i agree this will be a bit heavy...but if the file isnt too big, that wont b much problem...and nowdays almost all use memory >= 256mb , so i dont think that is a problem...
cheers,
sayon