0

For example,

Name    Local   International   Total
Monash University   42339   22140   64479
RMIT University 30843   26590   57433
University of Sydney    42028   12278   54306
University of New South Wales   39194   13132   52326
University of Melbourne 38091   14166   52257
University of Queensland    37252   11519   48771

I try the cat filename|sort -k 3 -n and the headline will end up in the middle, but I want to sort it by the number of international and keep the headline at the top. How to fix my pipeline?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
luven
  • 27
  • 6
  • 1
    Don't abuse `cat` like that — see [Useless Use of `cat`](https://stackoverflow.com/questions/11710552/). Are the fields in the file tab delimited? It isn't clear from the layout how you are supposed to detect the end of multi-word fields (like 'University of New South Wales'), but your `sort` certainly takes no account of that. – Jonathan Leffler Mar 13 '17 at 02:36

1 Answers1

1

You can use a literal tab as the separator character in the sort (using bash and ksh syntax)

$ <input.txt sed 1q ; \
<input.txt sed 1d | sort -t$'\t' -k 3,3 -n

portably:

$ <input.txt sed 1q ; \
<input.txt sed 1d | sort -t"$(printf '\t')" -k 3,3 -n

You can also convert the whitespace to some character you aren't using like %, sort the lines, and then convert it back when you're done.

$ <input.txt sed 1q ; \
<input.txt tr ' ' '%' | sed 1d | sort -k 3,3 -n | tr '%' ' '
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
  • I understand what you said, now I know how to use sed to differ the space and tab ,thank you! – luven Mar 15 '17 at 03:28
  • @luven You're welcome. In the example I was just using sed to skip the first line and print only the first line. The `sort -t <...>` option and `tr` are what handle the whitespace manipulation. – Greg Nisbet Mar 15 '17 at 03:36