0

How could i go printing the number of words in a specified file in a bash script. For example it will be run as

cat test | ./bash_script.sh

cat test

Hello World
This is a test

Output of running cat test | ./bash_script would look like

Word count: 6. 

I am aware that it can be done without a script. I am trying to implement wc -w into a bash script that will count the words like shown above. Any help is appreciated! Thank You

user2860658
  • 133
  • 2
  • 8
  • Thats 6 words! ;) http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/ http://stackoverflow.com/questions/5382712/bash-how-to-tokenize-a-string-variable These 2 links should help you greatly. The first one is how to tell the length of an array, the second is how to tokenize a string variable into an array – TheOneWhoPrograms Feb 03 '14 at 21:13
  • 5
    why not just have your bash script execute `wc -w`? This is *NOT* the kind of task that shell scripting is a good tool for, and while it is possible to drive a screw with a hammer that doesn't make it worth doing. – keshlam Feb 03 '14 at 21:14
  • one way is shown here http://stackoverflow.com/a/7045517/297323 but as other has stated, don't! – Fredrik Pihl Feb 03 '14 at 21:23
  • This is part of a script that counts how many words are on each line of a given file then at the end counts the total number of words in the file. – user2860658 Feb 03 '14 at 22:41

6 Answers6

3

if given a stream of input as shown:

while read -a words; do (( num += ${#words[@]} )); done
echo Word count: $num.

Extending from the link @FredrikPihl gave in a comment: this reads from each file given as an argument or from stdin if no files given:

for f in "${@:-/dev/stdin}"; do
    while read -a words; do (( num += ${#words[@]} )); done < "$f"
done
echo Word count: $num.

this should be faster:

for f in "${@:-/dev/stdin}"; do
    words=( $(< "$f") )
    (( num += ${#words[@]} ))
done
echo Word count: $num.
glenn jackman
  • 207,528
  • 33
  • 187
  • 305
1

in pure bash:

read -a arr -d $'\004'
echo ${#arr[@]}
ALiX
  • 991
  • 4
  • 9
1
#!/bin/bash
word_count=$(wc -w)
echo "Word count: $word_count."

As pointed by @keshlam in the comments, this can be easily done by executing wc -w from the shell script, I didn't understand what could be its use case.

Although, the above shell script will work as per your requirement. Below is a test output.

enter image description here

0

I believe what you need is a function that you could add to your bashrc:

function script1() {  wc -w $1; }

script1 README.md 
335 README.md

You can add the function to your .bash_rc file and call it what you want upon next console or if you source your .bashrc file then it will load in the function ... from then on you can call function name like you see with file and it will give you count

V H
  • 7,752
  • 2
  • 24
  • 43
0

You could expand the contents of the file as arguments and echo the number of arguments in the script.

$# Expands to the number of script arguments

#!/bin/bash

echo "Word count: $#."

Then execute:

./bash_script.sh $(cat file)

John B
  • 3,396
  • 1
  • 12
  • 20
0

Try this:
wc -w *.md | grep total | awk '{print $1}'