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. |
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. |
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 |
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? |
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.
|
I started with grep to get the correct recs, then sort & cut ;)
|
I read some example usage of sort command on IBM AIX Information Center website and figured it out.
Thank you. |
So as this site works on helping others, maybe you could post your result so everyone can benefit from your new found knowledge.
|
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. |
But the Q is
Quote:
|
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 |
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? |
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? |
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. |
Well, this is what I did
Code:
grep -E 'Poodle|JackRussel' t.t|sort -k5 -n -t,|cut -d',' -f1 (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. |