Vicky
Vicky

Reputation: 1328

Filter lines using grep -v

I am trying to filter out lines that don't contain the file names as below using the following command but I am not sure why line with permission denied keeps coming in my result. It should be gone when I have used grep -v "total|denied".

wc -l *.*   | egrep  -v "total|denied"  | sort -nr -k1,1

wc: host.save: Permission denied
33301 apache-maven-3.5.3-bin.tar.gz
14149 jenkins-cli.jar
 240 examples.desktop
  19 list.py
  19 interview_GL.sh
  17 lines.txt
   7 number.py

Upvotes: 1

Views: 1912

Answers (2)

Sven Klemm
Sven Klemm

Reputation: 436

Only stdout gets passed to the pipe into grep but those error messages are on stderr

You can either forward stderr to /dev/null or send them to stdout aswell

Send errors to /dev/null:

wc -l * 2>/dev/null

Redirect errors to stdout:

wc -l * 2>&1 | grep -v dir

Upvotes: 3

Mike Doe
Mike Doe

Reputation: 17566

You obviously aren't allowed to read host.save file's contents, therefore the error coming from the first command.

Have you tried muting the errors instead?

wc -l *.* 2>/dev/null | egrep  -v "total|denied"  | sort -nr -k1,1

Upvotes: 1

Related Questions