shell - Counting the Number of Characters in a File Using Bash and Assigning to Variable -
i need loop through text file provided , count number of characters in file. file contain 1 word supposed analyze. basically, need know length of word. want assign length variable have defined in bash file. here's have(ignore other if statement @ end, know working properly. need while loop):
#!/bin/bash file=$1 count=0 numchars=0 while ifs= read -rn1 char; if [[ "$char" == $'[a-za-z0-9#$+%@]' ]]; let numchars=numchars+1 fi done < "$file" if [ -r "${file}" ]; if /bin/egrep -q [09] $file ; let count=count+5 fi fi echo $count echo $numchars
the file contain 1 word
in case can do:
# read whole file in variable w="$(<"$file")" # length echo "length is: ${#w}"
option 2: using wc
len=$(( $(wc -c < "$file") - 1 ))
Comments
Post a Comment