LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 04-06-2019, 11:02 PM   #1
chronoscat
LQ Newbie
 
Registered: Apr 2019
Posts: 3

Rep: Reputation: Disabled
Question Help with sort command?


I apologize if this is the wrong place but I am new here. I have only been working with linux for a few months so I am pretty new. Currently I am having trouble with a script that I am writing. I have always had trouble with formatting sort commands and this one has me stumped.

For security purposes I have typed out an "example" file that is in no way related to the original file. This is my example (in no way related so pardon the ridiculousness of the content) :

item side menu# price
-------------------------
steak potato 3 10
chicken rice 2 8
steak fries 4 12
fish broccoli 1 9
steak eggs 5 11



###END EXAMPLE FILE###BEGIN MY DILEMMA###

in this script, I need to use the awk command to filter ONLY entries that contain "steak" as a menu item. (this I have working properly). Here is what I DON'T have working properly.

1. I need to restructure the columns so that when I run the script, they print in the order menu# (NOT in numerical order), item, side and then price
2. My steak list needs to be ordered by PRICE in ascending order and NOT by menu number.



I apologize if this is the wrong place or if this is really vague. I was instructed to use the sort command specifically.
 
Old 04-06-2019, 11:14 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by chronoscat View Post
I was instructed to use the sort command specifically.
The utility sort is definitely what you could use, with or without other pieces. Look around the Web for examples of using it with the -k option. Please also take a look at the manual page as well but just, in this case, for an example of a poor manual page. However, it will become useful as you get familiar with sort

What have you tried so far on this assignment?
 
Old 04-07-2019, 02:50 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
https://stackoverflow.com/questions/...olumns-in-bash
https://stackoverflow.com/questions/...with-unix-sort

here are two examples. But again, if you need help please show us what you have already implemented and we will try to help you to improve.
 
Old 04-07-2019, 09:45 AM   #4
chronoscat
LQ Newbie
 
Registered: Apr 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
this is my draft so far. I have not run it yet because I know it is a hot mess and probably not structured properly. I found a board that showed to use the 'awk' command to change the order of the columns.

*names have been changed to reflect my example file*

awk '/steak/' /home/USER/FOLDER/FOOD '{print $3,$1,$2,$4}' | sort -k 4n



***/home/USER/FOLDER/FOOD is the location of the file containing the table

*** '{print}' is my attempt at using the awk command to change the order of the columns after the command has been executed.
 
Old 04-07-2019, 10:55 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Code:
awk '/steak/ { print whatever }' filename
is the correct syntax
 
1 members found this post helpful.
Old 04-07-2019, 05:24 PM   #6
chronoscat
LQ Newbie
 
Registered: Apr 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you for your help! It works now! I really appreciate everything!
 
Old 04-08-2019, 01:30 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
glad to help you.
If your problem is solved, please mark the thread solved.
 
Old 04-08-2019, 01:41 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
You can also sort on more than one key. So if you don't need to exclude the other categories, you can sort them:

Code:
sort -k1,1 -k4,4n

tail -n +3 | sort -k1,1 -k4,4n
That second one skips the header.
 
  


Reply



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
Can I use GNU sort to sort one field in order, another in reverse? zombieite Linux - Newbie 4 04-27-2009 12:23 AM
php sort help - sort numerical descending then by alphabetical? RavenLX Programming 3 03-11-2009 08:35 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 07:44 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration