I have a variable which has a string stored in it and need to check if it has lines in it:
var=`ls "$sdir" | grep "$input"`
pseudo-code:
while [ ! $var's number of lines -eq 1 ] do something
That's my idea on how to check it. echo $var | wc -l
doesn't work - it always says 1
, even though it has 3
.
echo -e
doesn't work as well.
wc. 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.
Using “wc -l” There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output.
you can use wc to count the number of characters in the file wc -m filename.
The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal. The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.
Quotes matter.
echo "$var" | wc -l
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