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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.