Reputation: 5093
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"
Upvotes: 8
Views: 16578
Reputation: 24812
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)"
Upvotes: 9
Reputation: 45
echo "Error count found in logfile is "$(grep error logfile | wc -l)
Upvotes: -1
Reputation: 1073
Here are some more examples of different ways to handle this task, just to help you get a feel for bash's features and options:
(echo -en "Error count found in logfile is "; grep error logfile | wc -l)
{ printf "Error count found in logfile is " && wc -l < <(grep error logfile); }
printf 'Error count found in logfile is %d\n' $(grep -c error logfile)
awk '/error/{++c} END{printf "Error count found in logfile is %d\n", c}' logfile
var=$(grep -c error logfile); echo "Error count found in logfile is $var"
If you want to go out of your way to use a "here string":
cat<<<"Error count found in logfile is $(grep -c error logfile)"
Also, here's a bash builtin only option:
declare -i c=0
while read l; do
[[ $l == *error* ]] && ((++c))
done < logfile
echo "Error count found in logfile is $c"`
Upvotes: 1
Reputation: 85780
Use printf
and grep
with -c
flag for pattern count.
printf "Error count found in logfile is %s\n" "$(grep -chi error logfile)"
The flags used in GNU grep
-c, --count
Suppress normal output; instead print a count of matching lines for each input file.
With the -v, --invert-match option (see below), count non-matching lines.
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is
only one file (or only standard input) to search.
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files.
Upvotes: 2
Reputation: 439193
To simply embed the (stdout) output from a command in a string, without the need to also store that output in a variable, simply use a command substitution ($(...)
) inside a double-quoted string ("..."
):
echo "Error count found in logfile is $(grep error logfile | wc -l)"
As pointed out in Inian's answer, grep
supports counting matches directly, via its -c
option, so the above can be simplified to:
echo "Error count found in logfile is $(grep -c error logfile)"
Using a variable to store grep
's output is only needed if you need to refer to it again later:
var=$(grep -c error logfile)
echo "Error count found in logfile is $var"
# ... use $var again as needed
As for what you've tried: echo
only accepts command-line arguments, not stdin input, so using a pipeline (... | echo ...
) is the wrong approach.
Upvotes: 4