LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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

Reply
 
Search this Thread
Old 05-27-2009, 01:03 AM   #1
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Rep: Reputation: 0
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?
 
Old 05-27-2009, 01:06 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
what have you tried.
 
Old 05-27-2009, 01:25 AM   #3
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
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
$

Last edited by JulianTosh; 05-27-2009 at 01:27 AM.
 
Old 05-27-2009, 01:27 AM   #4
jdkaye
Senior Member
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 4,456

Rep: Reputation: Disabled
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
 
Old 05-27-2009, 01:35 AM   #5
thangappan
Member
 
Registered: May 2009
Posts: 52

Rep: Reputation: 16
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.
 
Old 05-27-2009, 01:39 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
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
 
Old 05-27-2009, 01:52 AM   #7
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
Is this php?

Hi,

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

Quote:
Originally Posted by Admiral Beotch View Post
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
$
 
Old 05-27-2009, 01:56 AM   #8
thangappan
Member
 
Registered: May 2009
Posts: 52

Rep: Reputation: 16
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

Last edited by thangappan; 05-27-2009 at 02:01 AM. Reason: I missed out join() function
 
Old 05-27-2009, 01:59 AM   #9
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
sorry, I didnt specify an interpreter... it's bash code.
 
Old 05-27-2009, 02:00 AM   #10
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
Sorting rows of a file

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

Quote:
Originally Posted by ghostdog74 View Post
what have you tried.
 
Old 05-27-2009, 02:08 AM   #11
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
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 View Post
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
 
Old 05-27-2009, 02:11 AM   #12
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
yup, thanks, i figured it out...
 
Old 05-27-2009, 02:16 AM   #13
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 15

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

Quote:
Originally Posted by chrism01 View Post
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
 
Old 05-27-2009, 02:26 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 05-27-2009, 03:06 AM   #15
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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
 
  


Reply

Tags
can, function, join, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to duplicate rows of text from one file to another? guest Programming 1 04-25-2009 08:14 AM
Print only specific rows in a text file Mike_V Programming 3 04-24-2009 07:18 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 05:49 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM


All times are GMT -5. The time now is 09:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration