LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux > 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

Tags used in this thread
Popular LQ Tags , , , , , , , , ,

Reply
 
Thread Tools
Old 05-27-2009, 02:03 AM   #1
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0
Sorting Rows of a file


[Log in to get rid of this advertisement]
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?
iframe is offline     Reply With Quote
Old 05-27-2009, 02:06 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,808
Blog Entries: 5
Thanked: 114
what have you tried.
ghostdog74 is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 02:25 AM   #3
Admiral Beotch
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 670
Blog Entries: 3
Thanked: 104
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 Admiral Beotch; 05-27-2009 at 02:27 AM..
Admiral Beotch is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 02:27 AM   #4
jdkaye
Senior Member
 
Registered: Dec 2008
Location: Gorizia
Distribution: Debian Testing
Posts: 1,791
Thanked: 158
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
jdkaye is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 02:35 AM   #5
thangappan
Member
 
Registered: May 2009
Posts: 37
Thanked: 2
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.
thangappan is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 02:39 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,411
Thanked: 324
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
chrism01 is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 02:52 AM   #7
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0

Original Poster
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
$
iframe is offline     Reply With Quote
Old 05-27-2009, 02:56 AM   #8
thangappan
Member
 
Registered: May 2009
Posts: 37
Thanked: 2
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 03:01 AM.. Reason: I missed out join() function
thangappan is offline  
Tag This Post , , , , , , , , ,
Reply With Quote
Thanked by:
Old 05-27-2009, 02:59 AM   #9
Admiral Beotch
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 670
Blog Entries: 3
Thanked: 104
sorry, I didnt specify an interpreter... it's bash code.
Admiral Beotch is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 03:00 AM   #10
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0

Original Poster
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.
iframe is offline     Reply With Quote
Old 05-27-2009, 03:08 AM   #11
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0

Original Poster
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
iframe is offline     Reply With Quote
Old 05-27-2009, 03:11 AM   #12
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0

Original Poster
yup, thanks, i figured it out...
iframe is offline     Reply With Quote
Old 05-27-2009, 03:16 AM   #13
iframe
LQ Newbie
 
Registered: Mar 2007
Posts: 9
Thanked: 0

Original Poster
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
iframe is offline     Reply With Quote
Old 05-27-2009, 03:26 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,808
Blog Entries: 5
Thanked: 114
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
ghostdog74 is offline     Reply With Quote
Thanked by:
Old 05-27-2009, 04:06 AM   #15
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 165
Thanked: 21
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
PMP is offline     Reply With Quote
Thanked by:

Reply

Bookmarks


Thread Tools

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 09:14 AM
Print only specific rows in a text file Mike_V Programming 3 04-24-2009 08:18 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 06:49 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 12:57 PM


All times are GMT -5. The time now is 09:19 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration