Sort by absolute value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort by absolute value
# 1  
Old 09-28-2011
Sort by absolute value

Hi,
I have a file as follows:
Code:
  |-30.0|Appls. executing in db manager currently   = 2
  |-80.0|Locks held currently                       = 1
  | 90.0|High water mark (bytes)                 = 65536
  |-50.0|Configured size (bytes)                 = 16777216
  |-100.0|Current size (bytes)                    = 131072

I want to sort on the absolute value of the second column with '|' as delimeter. sort -n sorts the field numerically but cannot sort by its absolute value ignoring the signs.

I would like to get an output as follows:
Code:
  |-30.0|Appls. executing in db manager currently   = 2
  |-50.0|Configured size (bytes)                 = 16777216
  |-80.0|Locks held currently                       = 1
  | 90.0|High water mark (bytes)                 = 65536
  |-100.0|Current size (bytes)                    = 131072

Please help.

Thanks,
Sudha
# 2  
Old 09-28-2011
Code:
$ sed 's/| \([^-]*\)|/|-\1-|/' file|sort -t'|' -k2,3 -nr|sed 's/|-\(.*\)-|/| \1|/'
  |-30.0|Appls. executing in db manager currently   = 2
  |-50.0|Configured size (bytes)                 = 16777216
  |-80.0|Locks held currently                       = 1
  | 90.0|High water mark (bytes)                 = 65536
  |-100.0|Current size (bytes)                    = 131072

# 3  
Old 09-28-2011
Perl:

Code:
perl -F'\|' -lane'
  push @d, [abs $F[1], $_];
  END {
    print join $/, map $_->[1], sort {
      $a->[0] <=> $b->[0] 
      } @d;  
  }' infile

# 4  
Old 09-28-2011
Get the 2nd column by "cut", then remove the "-" by "sed" and finally "sort -n".

Code:
cut -d '|' -f2 | sed 's/[^0-9.]//g' | sort -n

# 5  
Old 09-28-2011
Quote:
Originally Posted by MacMonster
Get the 2nd column by "cut", then remove the "-" by "sed" and finally "sort -n".

Code:
cut -d '|' -f2 | sed 's/[^0-9.]//g' | sort -n

this is the output based on what you've provided:
Code:
30.0
50.0
80.0
90.0
100.0

it's not what the OP wanted.
Code:
nawk -F'|' '{print ($2>=0)?$2:-$2, $0}' OFS='|' myFile | sort -n -k1,1 | cut -d '|' -f2-


Last edited by vgersh99; 09-28-2011 at 10:00 AM..
# 6  
Old 09-28-2011
Quote:
Originally Posted by vgersh99
this is the output based on what you've provided:
Code:
30.0
50.0
80.0
90.0
100.0

it's not what the OP wanted.
Code:
nawk -F'|' '{print ($2>0)?$2:-$2, $0}' OFS='|' myFile | sort -n -k1,1 | cut -d '|' -f2-

Sorry! You're right. I misunderstood the starter's meaning. Smilie
# 7  
Old 09-28-2011
With gawk (WHINY_USERS for gawk version 3.x, PROCINFO for gawk version 4):
Code:
WHINY_USERS=1 gawk -F\| '{
  x = sprintf("%6.1f", $2 > 0? $2 : -$2)
  if (x in a) a[x] = a[x] "\n" $0
  else a[x] = $0
}
END {
  PROCINFO["sorted_in"] = "@ind_num_asc"
  for (i in a) print a[i]
}' infile


Last edited by binlib; 09-28-2011 at 04:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use absolute function?

Hello All, I am using following awk command in my shell script. I want to compare the value in column 2 and colum 3 after taking their absolute value. Column $2 and $3 can have any value positive or negative or both. awk -F"|" '{print $0,($2>$3?"F":"T")}' OFS='|' myfile.txt Your help... (2 Replies)
Discussion started by: angshuman
2 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

5. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

6. UNIX for Dummies Questions & Answers

absolute value

is there any function in unix which will convert a integer to absolute value with a single decimal point. suppose x=15232 y=x/1024=14.875 i want y to be 14.8 Similarly if y=6.29452 it should come as 6.3 (3 Replies)
Discussion started by: dr46014
3 Replies

7. Shell Programming and Scripting

Absolute value

Is there a function in awk to get the absolute value of a-b which can be negative or positive, I just care for the absolute value... (5 Replies)
Discussion started by: placroix1
5 Replies

8. Shell Programming and Scripting

get absolute filename

How would I get the absolute filename of a selected file...I want to control click...I already have the context menu all set to run a script...I just need to be able to get the file name of the file I control clicked on. In windows it is as follows: set filename=%~f1 set name=%~n1 set... (0 Replies)
Discussion started by: mainegate
0 Replies

9. Programming

absolute path

How to find out the absolute path of a file in C/C++? Thanks (4 Replies)
Discussion started by: filedeliver
4 Replies
Login or Register to Ask a Question