0

My sorting doesn't work for numbers (k3 to k6) but it works for k1 and k2 which are alphabets. How to sort for k3 to k6? Thanks for you help.

data:

MacOS X for dummies:Mary Abraham:53.48:88:38

code:

awk -F":" '{ printf "%-30s %-20s %-10.2f %-10d %-10d %s\n", $1, $2, $3, $4, $5, "$"$3 * $5 }' BookDB.txt | sort -nk3

format:"TITLE | AUTHOR | PRICE | QTY AVBL | QTY SOLD | TOTAL SALES"

1
  • Not sure if this is available on Mac OS' version of sort but you can specify the separator field to ":" instead of the default whitespace: sort -t':' -n -k3,3 Commented Jan 29, 2011 at 1:46

1 Answer 1

1

Looks like the spaces in the book title and author name are causing sort to miscount the columns, If you print a seperator character like '|' between each field in awk, then you can use sed to temporarily replace all the spaces like so,

| sed -e 's/ /#/g' -e 's/|/ /g' | sort -nk3 | sed -e 's/ /|/g' -e 's/#/ /g'

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.