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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question

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
share|improve this answer

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:

#!/bin/sh
if [ "$#" -ne 2 ]
then
    echo >&2 "Usage: $0 pattern file"
    exit 1
fi
printf '%s: ' "$1"
grep -c "$1" "$2"
share|improve this answer

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.

share|improve this answer

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

string='Thenis'  
inputFile=example.txt  
outputFile=/dev/stdout 
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

share|improve this answer

Try writing script.sh:

#!/bin/bash

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

Details

  • 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
share|improve this answer

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}
share|improve this answer

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.