LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2009, 03:54 PM   #1
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Rep: Reputation: 15
Problem with sorting contents in a file - "10" being put up the top


Hi,

I'm having a bit of trouble thinking of an efficient way to sort a file. The file has contents such as this:

Code:
file1
file3
file5
file2
file4
file6
file7
file8
file11
file10
file9

using the sort command - i can sort most of it, however numbers "10" "11" etc. are being put on top, because they start with a 1 - so the resulting file is:

Code:
file1
file10
file11
file2
file3
file4
file5
file6
file7
file8
file9
is there some way around that?
 
Old 08-24-2009, 04:31 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
cat input.yxt | sed 's/e/e /' | sort --key=2 -g | sed 's/e /e/'
 
Old 08-24-2009, 05:17 PM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
No need to cat into sed; sed is perfectly capable of reading a file all on its own:
Code:
sed 's/e/e /' input.txt | sort --key=2 -g | sed 's/e /e/'
 
Old 08-24-2009, 07:17 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
There's no need to use sed
Code:
sort --key=1.5 -g input.txt
 
Old 08-25-2009, 07:19 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks both Nice bit of teamwork and made me chuckle how my "it works" solution was progressively refined to an elegant, minimal solution.
 
  


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
unable download the file through "put" command for anonymous user roni_atx Linux - General 3 11-20-2008 03:15 AM
No UTMPX entry, You must EXEC "login" for the lowest "shell" ooihc Solaris / OpenSolaris 7 03-12-2007 02:09 PM
bash, sorting mp3's, problem in "for" ... may be zomane Programming 3 10-06-2006 07:08 AM
Samba- on "put file x" says file does not exist? achtung_linux Linux - Software 3 08-22-2006 01:30 PM
K3b: - Howto re-dock "Directories" and "Contents" windows back into the main window? hagies Linux - Software 4 04-26-2006 08:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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