PDA

View Full Version : real silly sort problem



crownedzero
October 27th, 2011, 03:50 PM
trying to sort a file that contains monetary value in the 3rd field using a script but begins with a $:

sort -b (blank spaces) -n (numeric) -r (reverse) -k 3,2 (from field,letter) is sorting correcting except six figures beginning with 1 are placed at the bottom under 2 for example. I know it's something simple, where'd I trip up?

Arndt
October 27th, 2011, 04:34 PM
trying to sort a file that contains monetary value in the 3rd field using a script but begins with a $:

sort -b (blank spaces) -n (numeric) -r (reverse) -k 3,2 (from field,letter) is sorting correcting except six figures beginning with 1 are placed at the bottom under 2 for example. I know it's something simple, where'd I trip up?

Some example data, maybe?

crownedzero
October 27th, 2011, 04:49 PM
Simple shell script

#!/bin/bash/

echo Hello $LOGNAME

echo -e "\n"
more employee.txt

echo -e "\n"
echo -n Total number of employees:
sed -n '$=' employee.txt

echo -e "\n"

awk -F";" '{print $4, " ", $2, " " $1}' employee.txt | sort -nr

Column in question

$93100
$82001
$75600
$68500
$66050
$56500
$56500
$56000
$56000
$56000
$55508
$54500
$46000
$45210
$38000
$33930
$156003
$116400

emiller12345
October 27th, 2011, 06:31 PM
or you could just use a one-liner

cat temp.txt | sed 's/^\$//' | sort -n | awk '{print "$"$1}'

Arndt
October 27th, 2011, 07:13 PM
trying to sort a file that contains monetary value in the 3rd field using a script but begins with a $:

sort -b (blank spaces) -n (numeric) -r (reverse) -k 3,2 (from field,letter) is sorting correcting except six figures beginning with 1 are placed at the bottom under 2 for example. I know it's something simple, where'd I trip up?

It is -k 3.2 that you want.

crownedzero
October 27th, 2011, 07:40 PM
It is -k 3.2 that you want.

Damn these old eyes ... thanks!