LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-20-2012, 09:59 PM   #1
HaydeezPluto
LQ Newbie
 
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Rep: Reputation: Disabled
Question Which combination of commands to use for sorting and cutting?


There was this question at school which I couldn't figure the answer to.

This is the output of command: cat PetTable

Sammy,Poodle,12,Dead,10
Lizzie,Poodle,8,Alive,28
JamesBond,Goldfish,2,Dead,0
Gus,Turtle,4,Alive,1
Roxy,JackRussel,11,Alive,20
Ellie,Poodle,6,Alive,25

Q. Command to list the names of dogs sorted by their weight.


Name is the first field and weight is the last.

Thank You.
 
Old 11-20-2012, 11:03 PM   #2
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,196

Rep: Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120
Hi,

welcome to LQ. We will not do your homework for you. We will however help. So, what have you tried? What command line tools have you been taught about?

Evo2.
 
Old 11-21-2012, 12:02 AM   #3
descendant_command
Member
 
Registered: Mar 2012
Posts: 785

Rep: Reputation: 179Reputation: 179
man sort
man cut

Unlike some "desktop software" (bonobo or akonadi or ... whateverthehell) most of the GNU cli tools just do what they say on the tin.
 
Old 11-21-2012, 02:33 PM   #4
HaydeezPluto
LQ Newbie
 
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Original Poster
Rep: Reputation: Disabled
To sort the weight:

cut -d, -f5 PetTable | sort -n

can be done, but it discards the names. If I include the "name" field with cut, it won't sort. So?
 
Old 11-21-2012, 02:49 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Nope. You can't cut a column, you have to keep the input all together. sort has an option to select the field to sort (see -k) and another one to specify the field separator (see -t). Try them out.
 
1 members found this post helpful.
Old 11-21-2012, 05:25 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
I started with grep to get the correct recs, then sort & cut
 
Old 11-21-2012, 07:49 PM   #7
HaydeezPluto
LQ Newbie
 
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Original Poster
Rep: Reputation: Disabled
I read some example usage of sort command on IBM AIX Information Center website and figured it out.

Thank you.
 
Old 11-22-2012, 09:26 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
So as this site works on helping others, maybe you could post your result so everyone can benefit from your new found knowledge.
 
Old 11-22-2012, 01:11 PM   #9
HaydeezPluto
LQ Newbie
 
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Original Poster
Rep: Reputation: Disabled
Okay. I had seen the -t option in the help for sort, but wasn't sure how to use it. But, sort command can be used to specify the field which needs to be sorted while also choosing the delimiter. For the above problem,

sort -n -t, +4 PetTable | cut -d, -f1

worked well.
 
Old 11-23-2012, 12:38 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
But the Q is
Quote:
Q. Command to list the names of dogs(!) sorted by their weight.
which is why I also used grep ...
 
Old 11-23-2012, 09:13 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
Must be your system behaves different to mine as the following is the output I get from your code:
Code:
$ sort -n -t, +4 PetTable | cut -d, -f1
sort: open failed: +4: No such file or directory
 
Old 11-23-2012, 05:12 PM   #12
HaydeezPluto
LQ Newbie
 
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Original Poster
Rep: Reputation: Disabled
Could be the shell. Another way to do it is to use the -k option which defines the key, as colucix pointed it out above. So,

sort -n -t, -k5 PetTable | cut -d, -f1

works too. The -t option for sort works same as the -d option for cut. The character immediately following both options will define the delimiter. And -k option for sort seems same as -f option for cut. So, the digit immediately following them will be worked upon.

chrism01, can you please show how it can be done using grep command?

Last edited by HaydeezPluto; 11-23-2012 at 05:17 PM.
 
Old 11-23-2012, 11:36 PM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
hmmmm ... not to nitpick, but as chrism01 pointed out, your original question asked for the dogs to be sorted by weight and according to your input, Jamesbond is a goldfish and Gus is a turtle.
Should there not be more to your script to make this correct?
 
Old 11-25-2012, 11:42 AM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
The "+n" style syntax in core commands like sort, head, and tail is generally deprecated, and may not be fully supported.

The gnu versions of these utilities used in Linux also often have differences from the versions available in other Unixes. Most of these are simply expansions and improvements, but sometimes the original *nix or posix syntax needs to be explicitly enabled.

In short, don't just trust any examples you get off the web. Always check the documentation (man and info pages) and make sure the command is fully compatible with the program you're using.
 
Old 11-26-2012, 12:44 AM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Well, this is what I did
Code:
grep -E 'Poodle|JackRussel' t.t|sort -k5 -n -t,|cut -d',' -f1

Sammy
Roxy
Ellie
Lizzie
As grail noticed; the qn asks for dogs(!) only to be considered.
(Incidentally, Jack Russell has 2 x s http://www.jack-russell-terrier.co.u...d.php?20,16131)
 
  


Reply


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
Cutting down on Spam! sunshadow Linux - Server 4 05-29-2012 01:58 AM
Bind Mouse Button Combination to Keyboard Key Combination? neoAKiRAz Linux - Desktop 0 05-04-2007 12:49 PM
SBC is cutting out BajaNick General 0 06-27-2004 12:32 AM
mozilla 1.4 cutting out patrickfreen Red Hat 2 10-08-2003 10:57 AM
rpm2tgz not cutting it Kellvyn Slackware 2 07-28-2002 02:46 AM


All times are GMT -5. The time now is 07:48 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