spring cc spring cc - 5 months ago 10
Bash Question

how can I sort the line of a file according to the special part of the line

This is the file content (just a simplified example) which I want to sort:

1 33 blabla_0_banana
2 32333 lablab_4_apple
3 1232312 hahaah_1_banana
4 3342222 ohohoh_2_apple


And I want to sort the results with two requirement:


  1. firstly by the end word(eg: banana\apple)

  2. secondly by the number between the two "_" symbols : _[number]_ (eg: 0\4\1\2)



this is the the result I wanted:

4 3342222 ohohoh_2_apple
2 32333 lablab_4_apple
1 33 blabla_0_banana
3 1232312 hahaah_1_banana


And finally, I want to delete the line with the second number >100000, this is also the result I wanted:

2 32333 lablab_4_apple
1 33 blabla_0_banana


How can I do this? Maybe with the command 'sort' , 'awk' or others.

Answer

Using sort:

sort -t_ -k3 -k2n file

4 3342222 ohohoh_2_apple
2 32333   lablab_4_apple
1 33      blabla_0_banana
3 1232312 hahaah_1_banana

To keep only rows with 2nd column < 100000 use awk:

awk '$2<100000' file | sort -t_ -k3 -k2n
2 32333   lablab_4_apple
1 33      blabla_0_banana

Working Code Demo

Comments