3

So I have:

$ cat fruits
2 bananas
3 cherries
4 figs
5 dates
6 elderberries
7 apples
8 grapes

and

1 $ cat prices
2 2.18
3 4.11
4 1.69
5 4.52
6 1.73
7 1.01
8 1.09

Every line from 'fruits' corresponds with the same line from 'prices'. How I can sort the fruits in alphabetical order using cut `n paste, so that the 'prices' looks like or just prints out the following:

1 1.01
2 2.18
3 4.11
4 4.52
5 1.73
6 1.69
7 1.09
4
$ paste prices fruits | sort -k2 | cut -f1
1.01
2.18
4.11
4.52
1.73
1.69
1.09

paste combines the two files, line by line. sort -k2 sorts them on the second column (the fruit name). cut -f1 returns just the first column (the prices).

For the above, I assumed that the line numbers shown in the display of the fruits and prices files were an artifact of the display software and not part of the actual files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.