LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 12-08-2007, 05:04 PM   #1
Yogiz
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Rep: Reputation: 0
Help with sorting a file


Hi there,

I would like to ask how I could sort this nam input file;

Code:
+ 2.47 4 3 cbr 500 ------- 3 4.0 1.1 294 989
+ 2.47 1 2 cbr 500 ------- 2 1.0 5.0 394 990
+ 2.47 2 3 cbr 500 ------- 2 1.0 5.0 313 828
+ 2.471 3 2 cbr 500 ------- 3 4.0 1.1 292 985
+ 2.472 3 5 cbr 500 ------- 1 0.0 5.0 233 600
so that the output file is in ascending order by last column "989", "990" etc

Code:
+ 2.472 3 5 cbr 500 ------- 1 0.0 5.0 233 600
+ 2.47 2 3 cbr 500 ------- 2 1.0 5.0 313 828
+ 2.471 3 2 cbr 500 ------- 3 4.0 1.1 292 985
+ 2.47 4 3 cbr 500 ------- 3 4.0 1.1 294 989
+ 2.47 1 2 cbr 500 ------- 2 1.0 5.0 394 990
Thanks in advance for your input.

Yogiz
 
Old 12-08-2007, 05:17 PM   #2
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
Use sort(1) utility
 
Old 12-08-2007, 05:17 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
If the number of columns is fixed, e.g. each rows have always 12 fields, you can use sort with option -k to specify the position of the field to be sorted:
Code:
sort -n -k 12 input_file
 
Old 12-08-2007, 05:31 PM   #4
Yogiz
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Awsome thanks for that.

How would I go on about then extracting just the first column into a new file?
 
Old 12-08-2007, 05:33 PM   #5
Yogiz
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Awsome thanks for that.

How would I go on about then extracting just the second column into a new file?
For example;

2.472
2.47
2.471
2.47
2.47
 
Old 12-08-2007, 05:41 PM   #6
Yogiz
LQ Newbie
 
Registered: Dec 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Got it used

awk '{print $2}' filename > output

Love Linux
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Linux File Sorting linuxgodrh Linux - General 7 08-01-2007 10:33 AM
sorting a file in shell script shmuel_orgad Programming 11 05-20-2006 09:16 PM
Sorting data in output file lluciano Linux - General 2 08-19-2005 11:37 AM
Sorting a file containing dates subu_s Linux - General 2 06-03-2004 02:07 PM
File sorting problem Kerridis Linux - General 3 03-08-2004 11:44 PM


All times are GMT -5. The time now is 10:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration