How to reverse the output of the grep command?

10,419

Solution 1

grep just filters, man grep : print lines matching a pattern, to change order another tool must be used, as there's only two items maybe adding |sort or |sort -r (sort reverse) can help.

grep -E '"ServiceUserName"|"ServicePassword"' | sort -r >> user.txt

Solution 2

You can use and adapt the tac command below:

$ cat test.txt
"ServicePassword":fooooooooooo
"ServiceUserName":barrrrrrrrr

$ egrep '"ServicePassword"|"ServiceUserName"' test.txt | tac
"ServiceUserName":barrrrrrrrr
"ServicePassword":fooooooooooo
Share:
10,419
Андрей Ка
Author by

Андрей Ка

Updated on June 17, 2022

Comments

  • Андрей Ка
    Андрей Ка almost 2 years

    i have one problem with grep command. in my called script i use:

       "ServicePassword":fooooooooooo
       "ServiceUserName":barrrrrrrrr
    grep -E '"ServiceUserName"|"ServicePassword"' >> user.txt
    

    but in user.txt file i will get first "ServicePassword":fooooooooooo then "ServiceUserName":barrrrrrrrr How can i use grep command to reverse it to get first ServiceUserName then ServicePassword output in user.txt file ? I need it only this sequence. i try to change:

    grep -E '""ServicePassword""|"ServiceUserName"' >> user.txt
    

    and nothing.... maybe exist a better solution?