Sorting Rows of a file
Hi,
I have a file, data.csv, with n rows, each row containing 7 numeric entries, like: x3, x5, x1, x4, x7, x2, x6 x11, x13, x14, x8, x10, x12, x9 ... ... ... ... ... ... ... 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: x1, x2, x3, x4, x5, x6, x7 x8, x9, x10, x11, x12, x13, x14 ... ... ... ... ... ... ... All values are integers. Note: I don't want to sort by columns, I want to to sort by rows... Thanks in advance? |
what have you tried.
|
Code:
for i in $(cat /tmp/numbers.txt); $ cat /tmp/numbers.txt 4,2,5,3,1,6 9,7,10,8,11 $ /tmp/testnumbers 1,2,3,4,5,6 7,8,9,10,11 $ |
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 |
Logic
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. |
Post #3 won't work if there are spaces between the fields, as per OP. Need to start with
Code:
IFS=" |
Is this php?
Hi,
Thanks for your answer. Is this php code?... Quote:
|
Implemention in perl
In the above reply I have mentioned that logic.Using that logic the following code has been implemented.
Perl Code: my ($FH,$RH); open ($FH,"<csvfile") or die "Can't open a file:$!\n"; open ($RH,">>outputfile") or die "Can't open resultant file:$!\n"; my (@sfields,@fields,$string); while(<$FH>){ chomp; @fields = split(/,/,$_); @sfields = sort { $a <=> $b } @fields; $string = join(",",@sfields); print $RH "$string\n"; } close($FH); close($RH) Input: + cat csvfile 4,2,5,3,1,6 9,7,10,8,11 Output: + cat outputfile 1,2,3,4,5,6 7,8,9,10,11 |
sorry, I didnt specify an interpreter... it's bash code.
|
Sorting rows of a file
I have tried bash sort -n, but it sorts the file by columns...
Quote:
|
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:
|
yup, thanks, i figured it out...
|
Spaces are not relevant, since I can use
cut -d, --output-delimiter=" " \ -f1-7 file.csv > new.csv Quote:
|
if you have Python
Code:
#!/usr/bin/env python Code:
# ./test.py |
And if you have perl
do this :-) cat test.txt 1,5,7,8,3,6 10,34,67,1,2,0,5 command line perl -e 'while(<>){chomp; my @sorted = sort { $a <=> $b } split(",", $_); print join (",", @sorted); print "\n"}' test.txt 1,3,5,6,7,8 0,1,2,5,10,34,67 Cheers |
All times are GMT -5. The time now is 06:11 PM. |