Sorting of text files based on some fields in the individual records using Unix Scrip
Hi,
Did anyone knows how to sort the records in a text file using some specific fields? I need to sort a textfile using awk or unix scripting.
for eg : In the text file
1) 2) 3) 4)
as f h b
sk e v m
I want to order the individual records based on field (1) first, followed by field (2).
Can someone pls advise and help? Thanks a lot...
July 24, 2006 04:37 AM
The command you are looking for is *drums* 'sort'.
And since you are sorting with fields 1 and 2 and all lines are unique, you don't even need parameters.
And since you are sorting with fields 1 and 2 and all lines are unique, you don't even need parameters.
Use the sort command is what he meant. Read the man page and see if you have any questions left. Based on your description, simply executing
should do it.
sort yourfile.txt
should do it.
<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>
Oh...No...Using sort alone may not help as I require the different char positions to sort...For eg I may want to order by firstly, char position 1 to 10 and then followed by char position 28 to 45. If char position 1 to 10 is a tie, then i will sort according to the key represented by char 28 to 45
You can use the -k option multiple times. For instance, to sort using the fourth column as the first key and the third column as a secondary key you would do
sort -k 4,4 -k 3,3 file
sort -k 4,4 -k 3,3 file
But i cannot use the colum as defined by unix...i need to specify the char position instead to represent the column..any method to do this? Thanks a million
July 25, 2006 03:12 AM
Yes, there are methods for doing just that. You just need to read the manual.
The default field separator is no good for you? Change it!
The default ordering keys are no good for you? Change them!
Now go and learn something instead of getting readymade answers which do not really help you.
[rant]
And stop changing your requirements all the time if you really want your problem to be solved. But do realize that by doing so you are not going to learn much and have to rely on other peoples in the future, when you could instead solve it yourself and learn something in the process, making your future problems more easily solved by yourself.
[/rant]
The default field separator is no good for you? Change it!
The default ordering keys are no good for you? Change them!
Now go and learn something instead of getting readymade answers which do not really help you.
[rant]
And stop changing your requirements all the time if you really want your problem to be solved. But do realize that by doing so you are not going to learn much and have to rely on other peoples in the future, when you could instead solve it yourself and learn something in the process, making your future problems more easily solved by yourself.
[/rant]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement