Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pipe wc -l output to echo output

Tags:

bash

shell

I count errors in a log file by doing grep error logfile | wc -l

It outputs 10

I want to print Error count found in logfile is 10

I think need to pipe it thru echo but how can I append 10 to echo output?

I tried

 var="$(grep -i error logfile | wc -l)" | echo "Error count found in logfile is $var"
like image 795
GJain Avatar asked Jan 04 '23 17:01

GJain


1 Answers

you should not pipe the var into echo, but instead run them in sequence:

var="$(grep -i error * | wc -l)"
echo "Error count found in logfile is $var"

or you can define the variable just for the echo command, by doing with bash:

 var="$(grep -i error * | wc -l)" ; echo "Error count found in logfile is $var"

As said in the comments below, of course you can embed your command call in your echo statement:

echo "Error count found in logfile is $(grep -i error * | wc -l)"
like image 188
zmo Avatar answered Jan 12 '23 22:01

zmo