Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put find word count result into a variable

#!/bin/bash
find *.txt | wc -l;

this gives number of txt files. In my program how do I put this output into a variable say "count"

like image 389
Chirag Avatar asked Apr 13 '12 11:04

Chirag


People also ask

How do you store a word count in a variable in UNIX?

Create a variable to store the file path. Use wc –lines command to count the number of lines. Use wc –word command to count the number of words.

How do you count words in Shell?

Using wc command. wc command is used to know the number of lines, word count, byte and characters count etc. Count the number of words using wc -w. Here, “-w” indicates that we are counting words.

How do you count words in a string without wc in Linux?

Words could be counted with sed 's/[[:space:]]/\n/g' filename | grep -c .

How do I count lines in bash?

The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.


1 Answers

#!/bin/bash
count=$(find *.txt | wc -l)
like image 110
rid Avatar answered Oct 06 '22 11:10

rid