Sort function -- My First Unix Homework!

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Sort function -- My First Unix Homework!
# 1  
Old 09-16-2010
Sort function -- My First Unix Homework!

1. The problem statement, all variables and given/known data:
To sort a data


2. Relevant commands, code, scripts, algorithms:
The data provided is saved in dummy.txt and provided in LIBSVM format, but I think this information is redundant..
Code:
+1 1:2     2:4      4:3.2
-1 2:2.1   2:2.1   6:5.9
+1 1:-6    2:-4    4:2.2
-1 1:9.6   2:6.3   6:5.8
+1 1:2.98  2:5.6   4:8.1
-1 3:0.3   3:0.3   5:4.1


And we have to use functions cut, sort, or tr to sort the data. The sorted data should be in this form: (From my observation the data is only sorted by looking at the first field.)
Code:
+1 1:2      2:4     4:3.2
+1 1:-6     2:-4   4:2.2
+1 1:2.98   2:5.6  4:8.1
-1 2:2.1    2:2.1  6:5.9
-1 1:9.6    2:6.3  6:5.8
-1 3:0.3    3:0.3  5:4.1


3. The attempts at a solution (include all code and scripts):
I tried
Code:
sort -t ' ' -k 0, 1 dummy.txt
sort -n -k 1,1 -r dummy.txt

but they both sorted the data using the second field as well..

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Purdue University West Lafayette USA
Prof. Vishy, STAT598

Moderator's Comments:
Mod Comment Do not use PHP code tags when the data is not PHP code.

Last edited by eeweepoh; 09-16-2010 at 02:48 AM..
# 2  
Old 09-16-2010
Code:
sort -k1nr,1

# 3  
Old 09-16-2010
thanks a lot, there was in fact a typo in the homework assignment..
# 4  
Old 09-16-2010
Look up stable sort in the man page.
e.g. in GNU sort it is the -s option.....

Code:
$ sort -s -k1,1rn infile
+1 1:2     2:4      4:3.2
+1 1:-6    2:-4    4:2.2
+1 1:2.98  2:5.6   4:8.1
-1 2:2.1   2:2.1   6:5.9
-1 1:9.6   2:6.3   6:5.8
-1 3:0.3   3:0.3   5:4.1


Last edited by Scrutinizer; 09-17-2010 at 08:55 AM.. Reason: Added actual command and output for clarity
# 5  
Old 09-17-2010
I can't see a suitable "sort" command.
The output file is every line starting with "+1" in the order read followed by every line starting with "-1" in the order read.

One simple way to achive the output is to read the file twice, outputting first the "+1" records then the "-1" records.
Code:
for prefix in "+1" "-1"
do
        cat dummy.txt | while read line
        do
                case "${line}" in
                        "${prefix}"*)
                                echo "${line}"
                                ;;
                esac
        done
done

./scriptname
+1 1:2     2:4      4:3.2
+1 1:-6    2:-4    4:2.2
+1 1:2.98  2:5.6   4:8.1
-1 2:2.1   2:2.1   6:5.9
-1 1:9.6   2:6.3   6:5.8
-1 3:0.3   3:0.3   5:4.1


Last edited by methyl; 09-17-2010 at 08:40 AM.. Reason: correct input filename.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inconsistent results using sort function

Could you please advise on the following: I have two space-delimited files with 9 and 10 columns, respectively, with exactly the same values in column 1. However, the order of column 1 differs between the two files, so I want to sort both files by column 1, so that I can align them and... (6 Replies)
Discussion started by: aberg
6 Replies

2. UNIX for Dummies Questions & Answers

UNIX sort

Hi I have below pattern A: Apple 2 B:Bolls 4 total_count = 6 A: pens 4 B:Bags 4 total count = 8 A: pens 4 B:Bags 4 A: cells 6 A: jobs 6 Output I need : A: Apple 2 B:Bolls 4 total_count = 6 A: pens 4 B:Bags 4 total count = 8 A: cells 6 A: jobs 6 ... (7 Replies)
Discussion started by: pkkanduk
7 Replies

3. Shell Programming and Scripting

Sort function UNIX bug ???

Hello there i have a funny behiavor of the sort fonction, i try it out on different Solaris machine and i have the same issue. So i would like to see if there is a rationel explanation here is some data in a file:test.txt ,Test,RSD,RSD_Asset ,Test,RSD,RSD_Credit ,Test,RSD,RSD_Liab... (3 Replies)
Discussion started by: kykyboss
3 Replies

4. Programming

C++ compilation error when I use predicate friend function in the std::sort()

Hi, Can anyone tell me why the following program is giving compiler error when I use a friend function of a class as the comparison predicate for the third parameter of std::sort() algorithm? How to correct it, keep the 'friend' intact? #include <iostream> #include <vector> #include <list>... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

6. Shell Programming and Scripting

Sort with UNIX

I want to sort unique values of column 2 that has the maximum value at column 7. this is my file and desired output I have a file like this: AD008 AD0081010180947 101018 0947 0950 1010180947 1010180950 AD008 AD0081010180947 101018 0947 0956 1010180947 1010180956 AD008 AD0081010180947... (12 Replies)
Discussion started by: aydj
12 Replies

7. Shell Programming and Scripting

Perl function to sort a file based on key fields

Hi, I am new to PERL.I want to sort all the lines in a file based on 1,2 and 4th filelds. Can U suggest me a command/function in perl for this operation.. (5 Replies)
Discussion started by: karthikd214
5 Replies

8. UNIX for Dummies Questions & Answers

unix SORT

Hey guys. I am trying to sort a file by account number through UNIX. I have a few things but it seems to sort by account number AND sort everything after the account number. Help please. Thanks (5 Replies)
Discussion started by: ndoggy020
5 Replies

9. Shell Programming and Scripting

sort function in perl

Hi, here is my perl script.This script creates an array and is sorting it using the in-built sort function in perl. #!/usr/local/bin/perl my number=6; my @num_arr=(1,2,3,4,5); my @array=(23,"$number","Hello",2.345,@num_arr); #printing the array print... (2 Replies)
Discussion started by: DILEEP410
2 Replies

10. Programming

sort function

do any one knows where i can find an implementation in c for the sort function (2 Replies)
Discussion started by: dbargo
2 Replies
Login or Register to Ask a Question