Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
My question is: How can I sort each row in ascending order and output the resulting sorted file to, say, data_sorted.csv and the entries are sorted like:
If the resulting vertical alignment (columns) is of no interest to you then just about any programming language (unicon, perl, python, etc. etc.) could do this in 3 or 4 lines of code. I would convert each row into a list and then sort the list and print the result to an output file. csv files are text files and the output of the program would preserve the csv format so you could import that directly into the application you're using. I could write you the code if you wish.
Cheers,
jdk
I am telling the logic which can be applied using any programming language.
=> open a original file in read mode
=> open a resultant file in append mode
=> Parse a file line b line
=> one line contains one row (it has 7 fields)
=> Store the line in one array
=> Using sort operation sort the fields and append into resultant file.
=> Close both the files.
Would be great if you help me with a bash script...
I have tried:
while read line
do
sort -n
echo "$line"
done < "$file"
but desn't work....
Quote:
Originally Posted by jdkaye
If the resulting vertical alignment (columns) is of no interest to you then just about any programming language (unicon, perl, python, etc. etc.) could do this in 3 or 4 lines of code. I would convert each row into a list and then sort the list and print the result to an output file. csv files are text files and the output of the program would preserve the csv format so you could import that directly into the application you're using. I could write you the code if you wish.
Cheers,
jdk
#!/usr/bin/env python
def numeric_compare(x, y):
return int(x[1:])-int(y[1:])
for line in open("file"):
line =line.strip().split(", ")
line.sort(cmp=numeric_compare)
print ','.join(line)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.