I'm trying to put the number of lines in a file into an integer variable.
This is how i'm doing it
typeset -i x
x=`wc -l $1`
where $1 is the command line arg
The problem is that wc -l gives a number and the filename like: 5 blah
Is there a way to only put the number into x?
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.
x=$(wc -l < "$1")
This avoids a useless use of cat
and any forks, and would work on a path containing spaces and even newlines.
You could do cat $1 | wc -l
instead.
Or wc -l $1 | cut -d " " -f 1
.
wc -l $1 | awk '{print $1}'
with awk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With