LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Which combination of commands to use for sorting and cutting? (https://www.linuxquestions.org/questions/linux-newbie-8/which-combination-of-commands-to-use-for-sorting-and-cutting-4175438059/)

HaydeezPluto 11-20-2012 09:59 PM

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.

evo2 11-20-2012 11:03 PM

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.

descendant_command 11-21-2012 12:02 AM

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.
:D

HaydeezPluto 11-21-2012 02:33 PM

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?

colucix 11-21-2012 02:49 PM

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.

chrism01 11-21-2012 05:25 PM

I started with grep to get the correct recs, then sort & cut ;)

HaydeezPluto 11-21-2012 07:49 PM

I read some example usage of sort command on IBM AIX Information Center website and figured it out.

Thank you.

grail 11-22-2012 09:26 AM

So as this site works on helping others, maybe you could post your result so everyone can benefit from your new found knowledge.

HaydeezPluto 11-22-2012 01:11 PM

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.

chrism01 11-23-2012 12:38 AM

But the Q is
Quote:

Q. Command to list the names of dogs(!) sorted by their weight.
which is why I also used grep ...

grail 11-23-2012 09:13 AM

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


HaydeezPluto 11-23-2012 05:12 PM

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?

grail 11-23-2012 11:36 PM

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?

David the H. 11-25-2012 11:42 AM

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.

chrism01 11-26-2012 12:44 AM

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)


All times are GMT -5. The time now is 11:45 PM.