I am trying to make a bash script that greps value and output the count and the value together

Something like this

grep -c 'Thenis' example.txt > example.lst

But in that list file not only to show the count but also the value I have entered like this

Thenis: 123

6 Answers 6


You can start a sub shell and use $0 variable to do this.

bash -c 'echo "$0: $(grep -c $0 example.txt)"' 'Thenis' > example.lst

Since grep -c prints only the number, we need to prefix with the given pattern. This can be accomplished by using the shell builtin printf to print the pattern without a following newline. Then we let grep print the count (on the same line).

Using parenthesis to place both statements in a sub-shell allows all of the output to be redirected only once.

I would do it like this:

(printf '%s: ' Thenis; grep -c Thenis example.txt) > example.lst

If you wanted to make a simple script to do this:

if [ "$#" -ne 2 ]
    echo >&2 "Usage: $0 pattern file"
    exit 1
printf '%s: ' "$1"
grep -c "$1" "$2"

No one tried to use sed yet?

grep -c Thenis example.txt|sed s/^/Thenis:\ /  > example.lst

or if you want a more versatile solution and have gnu sed

echo Thenis -- example.txt | \
sed -re 's/^(.*) -- (.*)$/echo -n \1:;grep -c \1 \2/e'

a word of warning on that last one: powerful enough to shoot yourself in the foot.


You can automate this with variables. Also you can embed the command's output with the character `

In the line below, change outputFile=/dev/stdout to your own value,

string='Thenis'; inputFile=example.txt; outputFile=/dev/stdout;  echo "${string}: `grep -c ${string} ${inputFile}`" > ${outputFile}

Or in different lines

echo "${string}: `grep -c ${string} ${inputFile}`" > ${outputFile}

Also, for the quotes you can use either ' or ". For the variables either $var of ${var}, as you prefer.

Good luck


Try writing script.sh:


echo $1: $(grep -c "$1" "$2")

So if example.txt:

Then is, Then-is, Then's, Thenis, Thins,
Thesis, Thanes, Thence, Things,
Thenis, Thorns, Thane's

Run script.sh in your command prompt, saving its output to example.lst:

$ ./script.sh 'Thenis' example.txt > example.lst

The desired searchword: count result can then be found in example.lst:

$ cat example.lst
Thenis: 2


  • as with all scripts, ensure script is executable for example by issuing the command sudo chmod +x script.sh
  • bash positional parameters: $1 for the first argument passed to the script.sh, $2 for the second, and so on
  • $( ... ) is command substitution to get the results of the commands contained, out for use by the echo

You can use awk instead. It is much more flexible.

E.g. to emulate grep -c you can use:

awk '/Thenis/{total+=1}END{print "Thenis: ",total}'

Though please note, that in this case (this applies to grep as well) only lines with word Thenis are counted. It is not count of the word Thenis. In case you need to count number of Thenis words in file you can use following awk construction:

awk '/Thenis/{total+=gsub("Thenis","")}END{print "Thenis: ",total}

or the way with variables (to type Thenis only once)

awk -v word="Thenis" '$0 ~ word{total+=gsub(word,"")}END{print word,": ",total}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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