A solution using paste
+ sort
to get each product sorted by its price:
$ paste - - < file|sort -k 2nr
rice 16
apple 12
orange 4
Explanation
From paste
man:
Write lines consisting of the sequentially corresponding lines from
each FILE, separated by TABs, to standard output. With no FILE, or
when FILE is -, read standard input.
paste
gets the stream coming from the stdin
(your <file
) and figures that each line belongs to the fictional archive represented by -
, so we get two columns using - -
sort
use the flag -k 2nr
to get paste
output sorted by second column in reverse numerical order.