Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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'
share|improve this answer
    
Ofcourse, the message even has the word "error" in it! Thanks a lot! –  Carlito Aug 14 '12 at 18:12

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'

share|improve this answer

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.

share|improve this answer
    
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. –  Carlito Aug 14 '12 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.