can we get sorted grep output

5,409

It is not possible by only using grep. You have to use another tool e.g. sort:

$ grep -e apple -e mango *.txt | sort -t: -k2,2
1.txt:apple
3.txt:apple
2.txt:mango
4.txt:mango
Share:
5,409

Related videos on Youtube

christopher_bincom
Author by

christopher_bincom

Updated on September 18, 2022

Comments

  • christopher_bincom
    christopher_bincom over 1 year

    I have same: 1.txt with apple written in it, 2.txt with mango written in it, 3.txt with apple written in it, 4.txt with mango written in it.

    grep -e apple -e mango *.txt will give result as:

    1.txt: apple
    2.txt: mango
    3.txt: apple
    4.txt: mango
    

    But I need output as:

    1.txt: apple
    3.txt: apple
    2.txt: mango
    4.txt: mango
    

    Is it possible with just grep command ? -- without using any other command like sort or so

    • clerksx
      clerksx about 9 years
      No, and even if there was, it would be wrong. Use sort.
    • jordanm
      jordanm about 9 years
      No, grep does not perform sorting.