You need to know how many lines, words, or characters are in a given file.
Use the wc (word count) command with awk in a command substitution.
The normal output of wc is something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ 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:
1 |
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:
1 |
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.