2

I want to see how many packages from a list are not installed. I use archlinux, so I use pacman -Q to determine if a package is installed. If it is not installed, it returns a message like: "error: package 'package-name' was not found".

I thought I could use grep -c 'error' like this:

pacman -Q autoconf automake binutils bison fakeroot flex gcc libtool m4 make patch pkg-config | grep -c 'error'

But for me it returns:

error: package 'bison' was not found
error: package 'pkg-config' was not found
0

It returns 0, but I would like it to return 2. Even with awk it doesn't work:

pacman -Q autoconf automake binutils bison fakeroot flex gcc libtool m4 make patch pkg-config | awk '/error: / {count++} END {print count}'

For me, this returns:

error: package 'bison' was not found
error: package 'pkg-config' was not found

So my guess is that variable count doesn't even have a value since it doesn't get printed. I also tried wc -l, but that doesn't work, it just returns 0 for me.

What is the correct way to print the number of not-installed packages?

EDIT: I noticed when I do:

pacman -Q autoconf automake binutils bison fakeroot flex gcc libtool m4 make patch pkg-config > tempfile  
cat tempfile

The two rows containing the error message are not added to tempfile (the rest of the command output does). Thought this might help solving this problem.

3 Answers 3

4

The problem is the error messages are not send to STDOUT but STDERR. You have to explicitly redirect STDERR as well, e.g:

pacman -Q bison patch pkg-config 2>&1 | grep -c 'error'

If you have something like bash or zsh you can use |& to automatically redirect STDOUT as well as STDERR:

pacman -Q bison patch pkg-config |& grep -c 'error'
1
  • Ofcourse, the message even has the word "error" in it! Thanks a lot! Commented Aug 14, 2012 at 18:12
1

The errors are printed to stderr, not stdout. Your pipe (|) only sees the stdout output. You can redirect stderr to stdout before the pipe, and then all of your output will go through grep:

pacman -Q autoconf automake binutils bison fakeroot flex gcc libtool m4 make patch pkg-config 2>&1 | grep -c 'error'

-1

You can use wc -l to count the number of lines outputted.

So if pacman -Q autoconf automake binutils bison fakeroot flex gcc libtool m4 make patch pkg-config | grep -c 'error' is outputting:

error: package 'bison' was not found
error: package 'pkg-config' was not found

you can run pacman -Q autoconf automake binutils bison fakeroot flex gcc libtool m4 make patch pkg-config | grep -c 'error' | wc -l to count the number of times grep outputted a line.

1
  • I thought the same, but that doesn't work for me. Your last comments returns "1" for me, I think that is because it counts the last row (which has the 0 from the grep -c command). If I remove the -c from grep and pipe it to wc -l it still returns 0. Commented Aug 14, 2012 at 17:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.