LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sorting Rows of a file (https://www.linuxquestions.org/questions/linux-software-2/sorting-rows-of-a-file-728755/)

iframe 05-27-2009 01:03 AM

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?

ghostdog74 05-27-2009 01:06 AM

what have you tried.

JulianTosh 05-27-2009 01:25 AM

Code:

for i in $(cat /tmp/numbers.txt);
do
  echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//"
  echo
done


$ 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
$

jdkaye 05-27-2009 01:27 AM

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

thangappan 05-27-2009 01:35 AM

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.

chrism01 05-27-2009 01:39 AM

Post #3 won't work if there are spaces between the fields, as per OP. Need to start with
Code:

IFS="
"

ie a hard coded newline

iframe 05-27-2009 01:52 AM

Is this php?
 
Hi,

Thanks for your answer. Is this php code?...

Quote:

Originally Posted by Admiral Beotch (Post 3553981)
Code:

for i in $(cat /tmp/numbers.txt);
do
  echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//"
  echo
done


$ 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
$


thangappan 05-27-2009 01:56 AM

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

JulianTosh 05-27-2009 01:59 AM

sorry, I didnt specify an interpreter... it's bash code.

iframe 05-27-2009 02:00 AM

Sorting rows of a file
 
I have tried bash sort -n, but it sorts the file by columns...

Quote:

Originally Posted by ghostdog74 (Post 3553959)
what have you tried.


iframe 05-27-2009 02:08 AM

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 (Post 3553982)
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


iframe 05-27-2009 02:11 AM

yup, thanks, i figured it out...

iframe 05-27-2009 02:16 AM

Spaces are not relevant, since I can use
cut -d, --output-delimiter=" " \
-f1-7 file.csv > new.csv

Quote:

Originally Posted by chrism01 (Post 3554000)
Post #3 won't work if there are spaces between the fields, as per OP. Need to start with
Code:

IFS="
"

ie a hard coded newline


ghostdog74 05-27-2009 02:26 AM

if you have Python
Code:

#!/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)

output
Code:

# ./test.py
x1,x2,x3,x4,x5,x6,x7
x8,x9,x10,x11,x12,x13,x14


PMP 05-27-2009 03:06 AM

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.