Use the wc (word count) command with awk in a command substitution.
The normal output of wc is something like this:
$ wc data_file 5 15 60 data_file # Lines only $ wc -l data_file 5 data_file # Words only $ wc -w data_file 15 data_file # Characters (often the same as bytes) only $ wc -c data_file 60 data_file # Note 60B $ ls -l data_file -rw-r--r-- 1 jp users 60B Dec 6 03:18 data_file
You may be tempted to just do something like this:
data_file_lines=$(wc -l "$data_file")
That won’t do what you expect, since you’ll get something like
"5 data_file"
as the value. Instead,
try this:
data_file_lines=$(wc -l "$data_file" | awk '{print $1}')
If your version of wc is locale aware, the number of characters will not equal the number of bytes in some character sets.
18.191.168.203