LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-16-2016, 04:31 PM   #1
nenokmagic
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Rep: Reputation: Disabled
Bash Shell, awk and sorting.


Hello all!
I am kinda stuck right now and I just don't seem to be able to fathom my mistake.

I am attempting to sort k4, from highest to lowest starting with the number 5.

So I am using this file.

-----------------
- restaurants.txt

smart,Parks,6834948,9
italian,Bardellis,6573434,5
steakhouse,Nelsons Eye,6361017,8
steakhouse,Butchers Grill,6741326,7
smart,Joes,6781234,5
steakhouse,Bobs,1967837,3
steakhouse,Fergals,1593579,4
steakhouse,Miras,1472586,5

#I am attempting to sort it using
------
awk '$4<5' restaurants.txt | sort -rn -k4
-------
#Anyone able to enlighten me? All help would be appreciated.
 
Old 03-16-2016, 04:39 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Your main mistake is the delimiter. All Linux functions assume the delimiter is a whitespace or newline unless you tell it otherwise. You need to add the '-F,' argument to awk, and '-t,' argument to sort to switch to comma delimiters.
 
1 members found this post helpful.
Old 03-16-2016, 05:03 PM   #3
nenokmagic
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
I see, thanks for responding. That helped me out a lot, everything seems to be going smoother so far. By that I mean the -rn actually seems to be working. The awk '$4<5' doesn't appear to be removing anything under five though. But I figure that the problem is as you noted in the,
Quote:
All Linux functions assume the delimiter is a whitespace or newline unless you tell it otherwise.
 
Old 03-16-2016, 05:06 PM   #4
nenokmagic
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hah silly me, I forgot the -F it all worked out. Thank you!
 
Old 03-16-2016, 05:10 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
No problem!

You can mark the thread as "Solved" under Thread Tools at the top of the page if you're satisfied with the solution.
 
Old 03-17-2016, 01:55 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Might just be me, but after you set the delimiter correctly for awk you will only be print lines where the fourth field is less than 5. So you will not be 'removing anything under five though', you will
only print the items under 5.

As I say, I might have missed what you were trying to do?
 
  


Reply

Tags
awk, shell script



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
[bash][awk] command runs from shell, not from script JohnyDRipper Programming 2 03-09-2012 05:01 AM
awk and sorting BerzinTehvs Linux - Software 8 07-31-2010 08:44 AM
bash shell script using pidof, ps -ef, awk Joe1989 Linux - General 2 04-22-2009 11:01 AM
awk sort function not sorting from lowest to highest. skuz_ball Programming 4 08-09-2008 12:20 AM
pass value from awk back to bash shell cjs_pro Programming 2 03-07-2005 04:53 PM

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

All times are GMT -5. The time now is 12:07 AM.

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