3

I have two commands which both return numbers. For example:

cat `find -name \*.cd -print` | wc -l

cat `find -name \*.c -print` | wc -l

Let's say that the first one returns 10, the other 5.

What would the command which return the sum of them, without changing these commands, look like?

I need something like this:

cat `find -name \*.cd -print` | wc -l     +     cat `find -name \*.c -print` | wc -l

and it should return 15 in this case.

How I can do that?

0

3 Answers 3

5

That command will execute yours two commands and print the sum of the results.

echo $(($(cat `find -name \*.cd -print` | wc -l)  + $(cat `find -name \*.c -print` | wc -l)))

EDIT: As @Karoly Horvath commented it would be more readable if it's not a oneliner:

cd_count=$(cat `find -name \*.cd -print` | wc -l)
c_count=$(cat `find -name \*.c -print` | wc -l)
echo $(($cd_count + $c_count))
Sign up to request clarification or add additional context in comments.

2 Comments

in this case a one-liner is probably a bad idea.
@KarolyHorvath That's true ;) I've edited the answer.
2
$ expr $(echo 5) + $(echo 10)
15

Just replace the echo statements with your commands.

Comments

2

It's better to combine the two searches:

cat $(find -regex '.*\.cd?$') | wc -l

or

find -regex '.*\.cd?$' | xargs cat | wc -l

or, if you filenames can contain spaces:

find -regex '.*\.cd?$' -print0 | xargs -0 cat | wc -l

3 Comments

compact and fast one. +1. perhaps it would be better if add a $ in regex. in case the file name had more periods(.)...
In this case - good idea to combine the searches. However it doesn't answer the OP's question - "return the sum of them, without changing these commands"
@damgad: I didn't want to reiterate the other approaches. I posted this as an alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.