Friday, May 3, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 643  / 3 Years ago, mon, september 13, 2021, 10:19:28

I have a command example,



device list


which gives me all devices with their status as okay or error in the following format



[okay     ]:  you are listening on : 11
[error ]: not currently listening: 22
[error ]: not currently listening: 33
[okay ]: you are listening on : 111


I want to print only devices that has status as okay.



I tried using grep command as



device list | grep -r 'okay'
device list | grep -r '[okay ]'
device list | grep -r '^okay'
device list | grep 'okay'


But it didn't print any output.
How can I print the devices with status as okay.



Thanks in advance.


More From » command-line

 Answers
7

You don't have to use -r option with grep in this case, because (look at man grep):




-r, --recursive
Read all files under each directory, recursively, following
symbolic links only if they are on the command line. This is
equivalent to the -d recurse option.



You also have to redirect and errors from the standard output to be piped by the grep command using 2>&1 redirection:



device list 2>&1 | grep 'okay'


Or you can use |&:



device list |& grep 'okay'

[#26371] Tuesday, September 14, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
odenanno

Total Points: 207
Total Questions: 113
Total Answers: 94

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
odenanno questions
;