9

I started using sed recently. One handy way I use it is to ignore unimportant lines of a log file:

tail -f example.com-access.log | sed '/127.0.0.1/d;/ELB-/d;/408 0 "-" "-"/d;'

But when I try to use it similarly with find, the results aren't as expected. I am trying to ignore any line that contains "Permission denied" like this:

find . -name "openssl" | sed '/Permission denied/d;'

However, I still get a whole bunch of "Permission denied" messages in stdout.

EDIT

As mentioned in the correct answer below, the "Permission denied" messages are appearing in stderr and NOT stdout.

  • You should look into grep/egrep. – Michael Hampton Jul 10 '13 at 02:13
  • @MichaelHampton I don't see how grep would help me out here. –  Jul 10 '13 at 02:14
  • By filtering things out of the log file that you don't want to see, of course. – Michael Hampton Jul 10 '13 at 02:17
  • @MichaelHampton I'm not filtering anything out of a log file (except in the example), I'm trying to ignore lines returned by find. I can't write a regex that matches results that are unknown. –  Jul 10 '13 at 02:22
  • 1
    @Ben You can, however, exclude the results that you do know. (-v) – Andrew B Jul 10 '13 at 02:53
  • I have no idea what either of you are talking about. –  Jul 10 '13 at 03:22
  • @Ben Bluntly: grep -v 'Permission denied', grep -Ev '(Permission denied|kitchen sink)', etc. – Andrew B Jul 10 '13 at 03:42
  • @AndrewB I'm guessing you mean pipe the results of the find command through that grep command, as in: find . -name "openssl" | grep -v "Permission denied". That doesn't work. –  Jul 10 '13 at 04:16
  • @AndrewB Yes. Thank you very much. I appreciate it. I would just like to know how, specifically, you would use grep to do the same thing, as I believe that is what you are telling me is possible. –  Jul 10 '13 at 04:27
  • 2
    Last comment, because this is about to get locked. If this works, upvote with no reply. find . -name "openssl" 2>&1 | grep -v "Permission denied" – Andrew B Jul 10 '13 at 04:35
  • For understanding how this two output stream could work, take a look at this SO answer : http://stackoverflow.com/a/16283739/1765658 – F. Hauri - Give Up GitHub Jul 10 '13 at 10:14

2 Answers2

18

The problem is error ouput printed to stderr, so the sed command can't catch the input. The simple solution is: redirecting stderr to stdout.

find . -name "openssl" 2>&1 | sed '/Permission denied/d;'
cuonglm
  • 153,898
17

I'm not sure why you are trying to use sed to remove permission denied messages from output of find - unless you are trying to learn how to use sed.

I would simply run this instead:

find . -name "openssl" 2>/dev/null

Here, I'm redirecting stderr (file descriptor 2) over to /dev/null (refer to man null). In other words, 2>/dev/null simply discards everything written to stderr.

Srinidhi
  • 486