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