LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-15-2004, 06:09 PM   #1
nisson
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Rep: Reputation: 0
....How to use awk to sort....


How to use awk command in linux to sort
a class of float-type datas, which is saved as
in a file. Thanks !
 
Old 10-15-2004, 06:11 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Do you have an example of what you want to sort?
 
Old 10-15-2004, 09:34 PM   #3
nisson
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
For example, I have a file 2.dat, whch is shown as follows.
0 0 -0.000000
0 2 -0.067044
0 3 -2.114625
0 4 -3.520602
0 5 -5.153252
0 6 -3.830406
0 7 -5.566137
0 8 -7.199962
0 9 -8.227023
0 10 -6.939604
0 11 -8.229088
0 12 -9.169961
0 13 -9.821586
0 14 -8.801335
0 15 -9.677837
0 16 -10.200804
0 17 -9.708465
0 18 -9.827853
0 19 -9.895395
0 20 -9.746067
0 21 -9.560159
0 22 -9.148484

I want to produce a file 2.dat which is sorted according to the value of $3.

Thanks
 
Old 10-15-2004, 10:08 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
I'm not sure which way you want it to go. Try the sort command with or without -r
cat /home/file.txt | awk '{print $3}' | sort -nr > /home/file1.txt
 
Old 10-15-2004, 10:33 PM   #5
nisson
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks! How to add $1, $2 to the sorted file
since the sorted file just contain the $3 according to
your given command.
 
Old 10-15-2004, 10:41 PM   #6
nisson
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks! I have find a combined comands to
add $1,$2 to the file.
 
Old 10-16-2004, 04:05 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Ok that was a fun challenge! I never did figure it out but found a web site with the sollution. http://www.linuxfocus.org/English/Se...rticle103.html .
It's down the page in the problem / solution section.

Getting the program to sort $3 is easy enough but the trick is getting $1 and $2 into the output. With the sollution from that web site, $3 ends up in front of the others which I figured you wouldn't want so I added some more awk statements to get them back where they belong.

Code:
#!/bin/bash

stuff="/home/file.txt"
cat ${stuff} | \
while read num
do
awk '{printf $NF;$NF = " "; printf " "$0"\n" }' | sort -nr | \
awk '{printf $NF;$NF = " "; printf " "$0"\n" }' | \
awk '{printf $NF;$NF = " "; printf " "$0"\n" }' > file1.txt

done
 
Old 10-16-2004, 06:55 PM   #8
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,770

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
awk 2.dat ' $ {print $0 }' | sort -k 3 -n -r -o 2.dat 2.dat

This will read file 2.dat, print the entire record, sort on field 3, print in numerical order (reversed), output to 2.dat, read from 2.dat.
It generates an error message (I haven't figured out why yet) about not being able to open file 2.dat, but it also sorts 2.dat, from largest to smallest value. To sort smallest to largest, remove -r from the above command.
So. Error message or no error message, the file gets sorted.

PS. The sort works fine without getting awk involved.

Last edited by bigrigdriver; 10-16-2004 at 07:14 PM.
 
Old 10-16-2004, 07:42 PM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Yep, you're right and that's what I get for staying up too late.

Code:
awk '{print $0}' | sort -k 3 -nr file.txt > file1.txt

or even this...

sort -k 3 -nr file.txt > file1.txt
 
Old 10-16-2004, 10:15 PM   #10
nisson
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Thank both of you!
 
Old 10-16-2004, 11:30 PM   #11
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
You're welcome from here!
What did your solution look like?
 
Old 10-17-2004, 10:04 PM   #12
nisson
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
cat /home/file.txt | awk '{print $3,$1,$2}' | sort -nr > /home/file1.txt
awk '{print $2,$3,$1}' file1.txt >file2.txt

"sort -k 3 -nr file.txt > file1.txt" is more simple and good way to do it.

Last edited by nisson; 10-17-2004 at 10:07 PM.
 
Old 11-02-2012, 03:51 PM   #13
vipul_makwana
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
Smile That is correct but i have some modification :

Quote:
Originally Posted by bigrigdriver View Post
awk 2.dat ' $ {print $0 }' | sort -k 3 -n -r -o 2.dat 2.dat

This will read file 2.dat, print the entire record, sort on field 3, print in numerical order (reversed), output to 2.dat, read from 2.dat.
It generates an error message (I haven't figured out why yet) about not being able to open file 2.dat, but it also sorts 2.dat, from largest to smallest value. To sort smallest to largest, remove -r from the above command.
So. Error message or no error message, the file gets sorted.

PS. The sort works fine without getting awk involved.

First of all thanks for the answer it helped me.....but If you will change the command a bit than the Error message wont come. I have tried it myself.
The correct syntax would be.........
$ awk '{print $0 }' 2.dat | sort -t "|" -k 6 -n -r -o 2.dat 2.dat

that means don't put "$" in front of print option..............
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Sort() bendeco13 Programming 2 11-02-2005 11:26 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 02:57 PM
Hello (sort of...) Gethyn LinuxQuestions.org Member Intro 1 10-15-2004 12:10 PM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM
sort pantera Programming 5 05-26-2004 07:36 PM


All times are GMT -5. The time now is 03:36 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration